python 中__setattr__, __getattr__,__getattribute__, __call__使用方法

摘要:
当属性name可以通过正常机制追溯到时,__getattr__是不会被调用的。如果类还定义了__getattr__方法,除非通过__getattribute__显式的调用它,或者__getattribute__方法出现AttributeError错误,否则__getattr__方法不会被调用了。如下所示,ClassA中定义了__getattribute__方法,实例insA获取属性时,都会调用__getattribute__返回结果,即使是访问__dict__属性。classClassA:def__init__:self.classname=classnamedef__getattr__:returndef__getattribute__:returninsA=ClassAprint#print#print#object.__setattr__会拦截所有属性的的赋值语句。自己实现__setattr__有很大风险,一般情况都还是继承object类的__setattr__方法。#invoke__setattr__print#{}insA.tag='insA'#invoke__setattr__print#{}object.__delattr__Like__setattr__()butforattributedeletioninsteadofassignment.Thisshouldonlybeimplementedifdelobj.nameismeaningfulfortheobject.object.__dir__dir()作用在一个实例对象上时,__dir__会被调用。
object._getattr_(self, name)

拦截点号运算。当对未定义的属性名称和实例进行点号运算时,就会用属性名作为字符串调用这个方法。如果继承树可以找到该属性,则不调用此方法

实例instance通过instance.name访问属性name只有当属性name没有在实例的__dict__或它构造类的__dict__或基类的__dict__中没有找到,才会调用__getattr__。当属性name可以通过正常机制追溯到时,__getattr__是不会被调用的。如果在__getattr__(self, attr)存在通过self.attr访问属性,会出现无限递归错误。

classClassA(object):
    def __init__(self, classname):
        self.classname =classname
    def __getattr__(self, attr):
        return('invoke __getattr__', attr)
insA = ClassA('ClassA')
print(insA.__dict__) #实例insA已经有classname属性了
#{'classname': 'ClassA'}
print(insA.classname) #不会调用__getattr__
#ClassA
print(insA.grade) #grade属性没有找到,调用__getattr__
#('invoke __getattr__', 'grade')
object.__getattribute__(self, name)

实例instance通过instance.name访问属性name__getattribute__方法一直会被调用,无论属性name是否追溯到。如果类还定义了__getattr__方法,除非通过__getattribute__显式的调用它,或者__getattribute__方法出现AttributeError错误,否则__getattr__方法不会被调用了。如果在__getattribute__(self, attr)方法下存在通过self.attr访问属性,会出现无限递归错误。如下所示,ClassA中定义了__getattribute__方法,实例insA获取属性时,都会调用__getattribute__返回结果,即使是访问__dict__属性。

classClassA(object):
    def __init__(self, classname):
        self.classname =classname
    def __getattr__(self, attr):
        return('invoke __getattr__', attr)
    def __getattribute__(self, attr):
        return('invoke __getattribute__', attr)
insA = ClassA('ClassA')
print(insA.__dict__)
#('invoke __getattribute__', '__dict__')
print(insA.classname)
#('invoke __getattribute__', 'classname')
print(insA.grade)
#('invoke __getattribute__', 'grade')
object.__setattr__(self, name, value)

会拦截所有属性的的赋值语句。如果定义了这个方法,self.arrt = value 就会变成self.__setattr__("attr", value).这个需要注意。当在__setattr__方法内对属性进行赋值时,不可使用self.attr = value,因为他会再次调用self.__setattr__("attr", value),则会形成无穷递归循环,最后导致堆栈溢出异常。应该通过对属性字典做索引运算来赋值任何实例属性,也就是使用self.__dict__['name'] = value.

如果类自定义了__setattr__方法,当通过实例获取属性尝试赋值时,就会调用__setattr__。常规的对实例属性赋值,被赋值的属性和值会存入实例属性字典__dict__中。

classClassA(object):
    def __init__(self, classname):
        self.classname =classname
insA = ClassA('ClassA')
print(insA.__dict__)
#{'classname': 'ClassA'}

insA.tag = 'insA'    
print(insA.__dict__)
#{'tag': 'insA', 'classname': 'ClassA'}

如果类自定义了__setattr__,对实例属性的赋值就会调用它。类定义中的self.attr也同样,所以在__setattr__下还有self.attr的赋值操作就会出现无线递归的调用__setattr__的情况。自己实现__setattr__有很大风险,一般情况都还是继承object类的__setattr__方法。

classClassA(object):
    def __init__(self, classname):
        self.classname =classname
    def __setattr__(self, name, value):
        #self.name = value  # 如果还这样调用会出现无限递归的情况
        print('invoke __setattr__')
insA = ClassA('ClassA') #__init__中的self.classname调用__setattr__。
#invoke __setattr__
print(insA.__dict__)
#{}

insA.tag = 'insA'    
#invoke __setattr__
print(insA.__dict__)
#{}
object.__delattr__(self, name)
Like __setattr__() but for attribute deletion instead of assignment. This should only be implemented if del obj.name is meaningful for the object.
object.__dir__(self)

dir()作用在一个实例对象上时,__dir__会被调用。返回值必须是序列。dir()将返回的序列转换成列表并排序。

object.__call__(self[, args...])
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).

Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的。换句话说,我们可以把这个类的对象当作函数来使用,相当于重载了括号运算符。

classStudent(object):
    def __init__(self, name):
        self.name =name
    def __call__(self):
        print('My name is %s.' %self.name)
s = Student('Michael')
s()
#My name is Michael.

通过使用__setattr__,__getattr__,__delattr__可以重写dict,使之通过“.”调用键值。

class Dict(dict):
    '''
    通过使用__setattr__,__getattr__,__delattr__
    可以重写dict,使之通过“.”调用
    '''
    def __setattr__(self, key, value):
        print("In '__setattr__")
        self[key] = value
    def __getattr__(self, key):
        try:
            print("In '__getattr__")
            return self[key]
        except KeyError as k:
            return None
    def __delattr__(self, key):
        try:
            del self[key]
        except KeyError as k:
            return None
    # __call__方法用于实例自身的调用,达到()调用的效果
    def __call__(self, key):    # 带参数key的__call__方法
        try:
            print("In '__call__'")
            return self[key]
        except KeyError as k:
            return "In '__call__' error"
s = Dict()
print(s.__dict__)
# {}
s.name = "hello"    # 调用__setattr__
# In '__setattr__
print(s.__dict__) # 由于调用的'__setattr__', name属性没有加入实例属性字典中。
# {}
print(s("name"))    # 调用__call__
# In '__call__'
# hello
print(s["name"])    # dict默认行为
# hello
# print(s)
print(s.name)       # 调用__getattr__
# In '__getattr__
# hello
del s.name          # 调用__delattr__
print(s("name"))    # 调用__call__
# None

免责声明:文章转载自《python 中__setattr__, __getattr__,__getattribute__, __call__使用方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HTML5Audio/Video全解(疑难杂症)Android系统--输入系统(六)模拟输入驱动程序下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

windows下使用python googleprotobuf

  首先下载:protobuf-2.5.0.tar.gz 和protoc-2.5.0-win32.zip。两者的版本要对应; 将下载的google protobuf解压,会看到一个python目录,Windows下可将vsprojectsDebug下的protoc.exe拷贝到python目录下。 在cmd下,切换到该目录,执行python setup.p...

Python之pandas读取mysql中文乱码问题

# -*- coding: utf-8 -*- # author:baoshan import pandas as pd import pymysql config = { "host": "localhost", "port": 3306, "user": "root", "password": "12...

[Python自学] Flask框架 (3) (路由、CBV、自定义正则动态路由、请求处理流程、蓝图)

一、路由系统 1.浅析@app.route的源码 我们使用@app.route("/index")可以给视图函数加上路由映射。我们分析一下@app.route装饰器的实现源码: def route(self, rule, **options): def decorator(f): endpoint = options.pop("en...

Java调用Python脚本并获取返回值

在Java程序中有时需要调用Python的程序,这时可以使用一般的PyFunction来调用python的函数并获得返回值,但是采用这种方法有可能出现一些莫名其妙的错误,比如ImportError。在这种情况下可以采用另一种方法:使用Java的Runtime,像在命令行直接调用python脚本那样调用python程序。此时可以通过文件作为脚本参数来传递Py...

PyQt(Python+Qt)学习随笔:QTreeWidgetItem项下子项的指示符展示原则childIndicatorPolicy

老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项下可以有子项,如果存在子项,则父项的节点是否显示展开或折叠子项的提示符由属性childIndicatorPolicy控制。 childIndicatorPolicy属性的类型为枚举类型QT...

python原生结束线程的方法

Python的threading 本身没有提供.kill()方法来结束线程。但是只需要一个小小的变通就可以实现。 一个caller线程在里面调用需要控制时间的tHhandle线程并把tHhandle线程设置.setDaemon()方法为True。caller线程结束,tHandle线程就会跟着结束。 ? # encoding: UTF-8 impor...