1.__getattr__ 方法的作用:当调用不存在的属性,就会调用__getattr__()方法:

当一般位置找不到attribute的时候,会调用getattr,返回一个值或AttributeError异常。

class abc():

    def __getattr__(self, item):
print item abc().test

那么它能干什么呢?

sdk的链式调用就可以用它来实现

结果:

class Dir(object):
def __init__(self, path=""):
self.__path = path def __getattr__(self, path):
print "path:", path
print 1
print "self.__path:",self.__path
# :renfanzi
return Dir("%s/%s" %(self.__path, path)) def __call__(self, user):
# print 2
# print user
return self.__getattr__(":" + user) def __str__(self):
return self.__path # print Dir("GET ").users('renfanzi').repos
# print Dir("GET ").usersasdfasd

代码

详情链接:https://www.douban.com/note/304331534/

python __getattr__的更多相关文章

  1. python __getattr__ 巧妙应用

    在之前的文章有提到__getattr__函数的作用: 如果属性查找(attribute lookup)在实例以及对应的类中(通过__dict__)失败, 那么会调用到类的__getattr__函数, ...

  2. python __getattr__ & __getattribute__ 学习

    实例属性的获取和拦截, 仅对实例属性(instance, variable)有效, 非类属性 getattr: 适用于未定义的属性, 即该属性在实例中以及对应的类的基类以及祖先类中都不存在 1. 动态 ...

  3. Python - __getattr__和__getattribute__的区别

    传送门 https://docs.python.org/3/reference/datamodel.html#object.__getattr__ https://docs.python.org/3/ ...

  4. python __getattr__ __setattr__

    class Rectangle: def __init__(self): self.width = 0 self.height = 0 def __setattr__(self, key, value ...

  5. python __getattr__和 __getattribute__

    __getattr__ 这个魔法函数会在类中查找不到属性时调用 class User: def __init__(self): self.info = 1 def __getattr__(self, ...

  6. python中__getattr__和__setattr__

    代码: #!/usr/bin/env python #! -*- coding:utf-8 -*- class A(object): def __setattr__(self, key, value) ...

  7. python 学习笔记7(类/对象的属性;特性,__getattr__)

    27. 属性的__dict__系统 1)对象的属性可能来自: 其类的定义,叫做类属性 继承父类的定义 该对象实例定义(初始化对象时赋值),叫做对象属性 2)对象的属性存储在对象的 __dict__ 属 ...

  8. python __setattr__, __getattr__, __delattr__, __call__

    python __setattr__, __getattr__, __delattr__, __call__ getattr `getattr`函数属于内建函数,可以通过函数名称获取 value = ...

  9. python面对对象编程-------5:获取属性的四种办法:@property, __setattr__(__getattr__) ,descriptor

    一:最基本的属性操作 class Generic: pass g= Generic() >>> g.attribute= "value" #创建属性并赋值 > ...

随机推荐

  1. zabbix server 在配置客户端的时候 在zabbix server端的/etc/hosts文件的hostname 对应的ip这样可以更快的让server端发现agent端

    如下: # cat /etc/hosts 127.0.0.1 localhost.localdomain localhost.localdomain localhost4 localhost4.loc ...

  2. [Windows Azure] Building the web role for the Windows Azure Email Service application - 3 of 5

    Building the web role for the Windows Azure Email Service application - 3 of 5. This is the third tu ...

  3. python __name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  4. 每日英语:Foreign Tourists Skip Beijing

    Overseas tourists continued to shun Beijing through 2013. shun:避开,避免,回避 Amid rising pollution and a ...

  5. git(8):常用命令

    Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v ...

  6. python(39):argparse的用法,从外部传入指定参数

    直接上例子: # /usr/bin/env python # coding=utf8 import os import argparse import logging import sys FORMA ...

  7. 分享几个免费IP地址查询API接口

    几个免费IP地址查询API接口 1.IP地址查询接口:http://apis.juhe.cn/ip/ip2addr要先去https://www.juhe.cn/docs/api/...申请APPKEY ...

  8. 阿里druid连接池监控配置

    首先在web.xml中添加如下配置: <filter> <filter-name>DruidWebStatFilter</filter-name> <filt ...

  9. Python实现二叉树的左中右序遍历

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/3/18 12:31 # @Author : baoshan # @Site ...

  10. maven 使用记录之修改 maven默认jdk版本

    maven package执行的时候会遇到jdk版本不对的问题 :原因是 maven所指定的jdk版本与项目使用的jdk版本不一致 1.项目属性的 java compiler可以设置 2.直接修改 m ...