您的位置:首页 > 编程语言 > Python开发

python 那些待深入理解的东西

2017-03-10 00:13 393 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: