python 那些待深入理解的东西

    xiaoxiao2021-03-25  89

    class Father:#2.7版本的老式类 def __init__(self): pass def function(self): print(type(self)) print(isinstance(self, Son)) print(isinstance(self, Father)) print(self.mlist) class Son(Father): def __init__(self): self.mlist = [1, 2, 3] def function(self): Father.function(self) s = Son() s.function()

    输出:

    <type 'instance'> True True [1, 2, 3] 这个例子程序中,有一点颠覆了我最近学习python后建立在大脑中的知识大厦。原来以为python中类的方法中的self关键字只是一个书写习惯,在调用类的方法的时候根本无需关心。然后在这个例子中却发现self的值使Father的function方法中的内容正常执行了。非常奇妙。

    由此,这个特性也用来解决了用户重载新式类中__getattribute__方法时无限递归__getattribute__方法自己的bug

    转载请注明原文地址: https://ju.6miu.com/read-17573.html

    最新回复(0)