[Python之路] object类中的特殊方法

一、object类的源码

python版本:3.8

class object:      """ The most base type """        # del obj.xxx或delattr(obj,'xxx')时被调用,删除对象中的一个属性      def __delattr__(self, *args, **kwargs):  # real signature unknown          """ Implement delattr(self, name). """          pass        # 对应dir(obj),返回一个列表,其中包含所有属性和方法名(包含特殊方法)      def __dir__(self, *args, **kwargs):  # real signature unknown          """ Default dir() implementation. """          pass        # 判断是否相等 equal ,在obj==other时调用。如果重写了__eq__方法,则会将__hash__方法置为None      def __eq__(self, *args, **kwargs):  # real signature unknown          """ Return self==value. """          pass        # format(obj)是调用,实现如何格式化obj对象为字符串      def __format__(self, *args, **kwargs):  # real signature unknown          """ Default object formatter. """          pass        # getattr(obj,'xxx')、obj.xxx时都会被调用,当属性存在时,返回值,不存在时报错(除非重写__getattr__方法来处理)。      # 另外,hasattr(obj,'xxx')时也会被调用(估计内部执行了getattr方法)      def __getattribute__(self, *args, **kwargs):  # real signature unknown          """ Return getattr(self, name). """          pass        # 判断是否大于等于 greater than or equal,在obj>=other时调用      def __ge__(self, *args, **kwargs):  # real signature unknown          """ Return self>=value. """          pass        # 判断是否大于 greater than,在obj>other时调用      def __gt__(self, *args, **kwargs):  # real signature unknown          """ Return self>value. """          pass        # 调用hash(obj)获取对象的hash值时调用      def __hash__(self, *args, **kwargs):  # real signature unknown          """ Return hash(self). """          pass        def __init_subclass__(self, *args, **kwargs):  # real signature unknown          """          This method is called when a class is subclassed.            The default implementation does nothing. It may be          overridden to extend subclasses.          """          pass        # object构造函数,当子类没有构造函数时,会调用object的__init__构造函数      def __init__(self):  # known special case of object.__init__          """ Initialize self.  See help(type(self)) for accurate signature. """          pass        # 判断是否小于等于 less than or equal,在obj<=other时调用      def __le__(self, *args, **kwargs):  # real signature unknown          """ Return self<=value. """          pass        # 判断是否小于 less than,在obj<other时调用      def __lt__(self, *args, **kwargs):  # real signature unknown          """ Return self<value. """          pass        # 创建一个cls类的对象,并返回      @staticmethod  # known case of __new__      def __new__(cls, *more):  # known special case of object.__new__          """ Create and return a new object.  See help(type) for accurate signature. """          pass        # 判断是否不等于 not equal,在obj!=other时调用      def __ne__(self, *args, **kwargs):  # real signature unknown          """ Return self!=value. """          pass        def __reduce_ex__(self, *args, **kwargs):  # real signature unknown          """ Helper for pickle. """          pass        def __reduce__(self, *args, **kwargs):  # real signature unknown          """ Helper for pickle. """          pass        # 如果不重写__str__,则__repr__负责print(obj)和交互式命令行中输出obj的信息      # 如果重写了__str__,则__repr__只负责交互式命令行中输出obj的信息      def __repr__(self, *args, **kwargs):  # real signature unknown          """ Return repr(self). """          pass        # 使用setattr(obj,'xxx',value)、obj.xxx=value是被调用(注意,构造函数初始化属性也要调用)      def __setattr__(self, *args, **kwargs):  # real signature unknown          """ Implement setattr(self, name, value). """          pass        # 获取对象内存大小      def __sizeof__(self, *args, **kwargs):  # real signature unknown          """ Size of object in memory, in bytes. """          pass        # 设置print(obj)打印的信息,默认是对象的内存地址等信息      def __str__(self, *args, **kwargs):  # real signature unknown          """ Return str(self). """          pass        @classmethod  # known case      def __subclasshook__(cls, subclass):  # known special case of object.__subclasshook__          """          Abstract classes can override this to customize issubclass().            This is invoked early on by abc.ABCMeta.__subclasscheck__().          It should return True, False or NotImplemented.  If it returns          NotImplemented, the normal algorithm is used.  Otherwise, it          overrides the normal algorithm (and the outcome is cached).          """          pass      # 某个对象是由什么类创建的,如果是object,则是type类<class 'type'>      __class__ = None      # 将对象中所有的属性放入一个字典,例如{'name':'Leo','age':32}      __dict__ = {}      # 类的doc信息      __doc__ = ''      # 类属于的模块,如果是在当前运行模块,则是__main__,如果是被导入,则是模块名(即py文件名去掉.py)      __module__ = ''

二、常用特殊方法解释

1.__getattribute__方法

1)什么时候被调用

这个特殊方法是在我们使用类的对象进行obj.属性名getattr(obj,属性名)来取对象属性的值的时候被调用。例如:

class Foo(object):      def __init__(self):          self.name = 'Alex'        def __getattribute__(self, item):          print("__getattribute__ in Foo")          return object.__getattribute__(self, item)      if __name__ == '__main__':      f = Foo()      print(f.name)  # name属性存在  或者 getattr(f,name)      print(f.age)  # age属性不存在

不管属性是否存在,__getattribute__方法都会被调用。如果属性存在,则返回该属性的值,如果属性不存在,则返回None。

注意,我们在使用hasattr(obj,属性名)来判断某个属性是否存在时,__getattribute__方法也会被调用。

2)与__getattr__的区别

我们在类的实现中,可以重写__getattr__方法,那么__getattr__方法和__getattribute__方法有什么区别??

我们知道__getattribute__方法不管属性是否存在,都会被调用。而__getattr__只在属性不存在时调用,默认会抛出 AttributeError: Foo object has no attribute age 这样的错误,但我们可以对其进行重写,做我们需要的操作:

class Foo(object):      def __init__(self):          self.name = 'Alex'        def __getattribute__(self, item):          print("__getattribute__ in Foo")          return object.__getattribute__(self, item)        def __getattr__(self, item):          print("%s不存在,但我可以返回一个值" % item)          return 54      if __name__ == '__main__':      f = Foo()      print(f.name)  # name属性存在      print(f.age)  # age属性不存在,但__getattr__方法返回了54,所以这里打印54。

返回结果:

__getattribute__ in Foo  Alex  __getattribute__ in Foo  age不存在,但我可以返回一个值  54

我们看到,f.name和f.age都调用了__getattribute__方法,但是只有f.age时调用了__getattr__方法。所以,我们可以利用__getattr__做很多事情,例如从类中的一个字典中取值,或者处理异常等。

2.__setattr__方法

当我们执行obj.name=’alex’setattr(obj,属性名,属性值),即为属性赋值时被调用。

class Foo(object):      def __init__(self):          self.name = 'Alex'        # obj.xxx = value时调用      def __setattr__(self, key, value):          print('setattr')          return object.__setattr__(self, key, value)      if __name__ == '__main__':      f = Foo()      f.name = 'Jone'  # 打印setattr      print(f.name)

如果__setattr__被重写(不调用父类__setattr__的话)。则使用obj.xxx=value赋值就无法工作了。

特别注意,在类的构造函数中对属性进行初始化赋值时也是调用了该方法:

class Foo(object):      def __init__(self):          self.name = 'Alex'  # 这里也要调用__setattr__    ...

当我们需要重写__setattr__方法的时候,就要注意初始化时要使用object类的__setattr__来初始化:

class Local(object):      def __init__(self):          # 这里不能直接使用self.DIC={},因为__setattr__被重写了          object.__setattr__(self, 'DIC', {})        def __setattr__(self, key, value):          self.DIC[key] = value        def __getattr__(self, item):          return self.DIC.get(item, None)      if __name__ == '__main__':      obj = Local()      obj.name = 'Alex'  # 向DIC字典中存入值      print(obj.name)  # 从DIC字典中取出值

3.__delattr__方法

这个方法对应del obj.属性名delattr(obj,属性名)两种操作时被调用。即,删除对象中的某个属性。

if hasattr(f,'xxx'):  # 判断f对象中是否存在属性xxx      delattr(f, 'xxx')  # 如果存在则删除。当xxx不存在时删除会报错      # del f.xxx  # 同上

4.__dir__方法

对应dir(obj)获取对象中所有的属性名,包括所有的属性和方法名。

f = Foo()  print(f.__dir__())  # ['name', '__module__', '__init__', '__setattr__', '__getattribute__', '__dir__', '__dict__', '__weakref__', '__doc__', '__repr__', '__hash__', '__str__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__class__']

返回一个列表。

5.__eq__和__hash__

__eq__是判断obj==other的时候调用的,默认调用的是object继承下去的__eq__。

f1 = Foo()  f2 = f1  print(f1 == f2)  # True  print(f1 is f2)  # True  print(hash(f1) == hash(f2))  # True

默认情况下,f1 == f2,f1 is f2,hash(f1)==hash(f2)都应该同时为True(或不相等,同为False)。

如果我们重写了__eq__方法,例如两个对象的比较变成比较其中的一个属性:

class Foo(object):      def __init__(self):          self.name = 'Alex'  # 这里也要调用__          self.ccc = object.__class__      def __eq__(self, other):          return self.name==other.name

即,如果self.name==other.name,则认为对象相等。

f1 = Foo()  f2 = Foo()  print(f1 == f2)  # True  print(f1 is f2)  # False  print(hash(f1) == hash(f2))  # 抛出异常TypeError错误

为什么hash会抛出异常,这是因为如果我们在某个类中重写了__eq__方法,则默认会将__hash__=None。所以,当我们调用hash(obj)时,__hash__方法无法执行。

 

总结:

当我们实现的类想成为不可hash的类,则可以重写__eq__方法,然后不重写__hash__,__hash__方法会被置None,该类的对象就不可hash了。

默认提供的__hash__方法(hash(obj))对于值相同的变量(类型有限制,有些类型不能hash,例如List),同解释器下hash值相同,而不同解释器下hash值不同。所以,如果我们想要hash一个目标,应该使用hashlib模块

hash和id的区别,理论上值相同的两个对象hash值应该相同,而id可能不同(必须是同一个对象,即内存地址相同,id才相同。id(obj)是obj的唯一标识。)

6.__gt__、__lt__、__ge__、__le__

这几个都是用于比较大小的,我们可以对其进行重写,来自定义对象如何比较大小(例如只比较对象中其中一个属性的值)。

7.__str__和__repr__

__str__用于定义print(obj)时打印的内容。

class Foo(object):      def __init__(self):          self.name = 'Alex'        def __str__(self):          return "我是Foo"      if __name__ == '__main__':      f1 = Foo()      print(f1)  # 打印 我是Foo

在命令行下:

>>> class Foo(object):  ...     def __str__(self):  ...             return "我是Foo"  ...  >>> f1 = Foo()  >>> print(f1)  我是Foo  >>> f1  <__main__.Foo object at 0x0000023BF701C550>

可以看到,使用__str__的话,print可以打印我们指定的值,而命令行输出则是对象的内存地址。

__repr__用于同时定义python命令行输出obj的内容,以及print(obj)的打印内容(前提是没有重写__str__)。

class Foo(object):      def __init__(self):          self.name = 'Alex'        def __repr__(self):          return "我是Foo"      if __name__ == '__main__':      f1 = Foo()      print(f1)  # 打印 我是Foo

在命令行下:

>>> class Foo(object):  ...     def __repr__(self):  ...             return "我是Foo"  ...  >>> f1 = Foo()  >>> print(f1)  我是Foo  >>> f1  我是Foo

可以看到,我们只重写了__repr__,但是print和直接输出都打印了我们指定的值。

当我们同时重写__str__和__repr__时:

>>> class Foo():  ...     def __str__(self):  ...             return "我是Foo---str"  ...     def __repr__(self):  ...             return "我是Foo---repr"  ...  >>> f1 = Foo()  >>> print(f1)  我是Foo---str  >>> f1  我是Foo---repr

可以看到,在同时重写两个方法时,__str__负责print的信息,而__repr__负责命令行直接输出的信息。

8.__new__方法

未完

9.__sizeof__方法

未完

10.__class__、__dict__、__module__、__doc__属性

__class__:返回该生成该对象的类

print(f1.__class__)  # <class '__main__.Foo'>

__dict__:返回该对象的所有属性组成的字典

print(f1.__dict__)  # {'name': 'Alex'} 只有一个属性name

__module__:返回该对象所处模块

class Foo(object):      def __init__(self):          self.name = 'Alex'      if __name__ == '__main__':      f1 = Foo()      print(f1.__module__)  # 打印__main__

如果该对象对应的类在当前运行的模块,则打印__main__。

import test3    f = test3.Foo()  print(f.__module__)  # 打印test3

如果对象对应的类在其他模块,则打印模块名。

__doc__:类的注释

class Foo(object):      """      这是一个类,名叫Foo      """      def __init__(self):          self.name = 'Alex'      if __name__ == '__main__':      f1 = Foo()      print(f1.__doc__)  # 打印 这是一个类,名叫Foo

 

 

 

 

###