时间:2021-07-01 10:21:17 帮助过:44人阅读
class Foo(object):
def __new__(cls, *args, **kwargs):
"""如果不覆盖这个__new__方法,也就是说不写这个__new__方法,类会从object
继承__new__方法完成返回值实例对象
"""
print "__new__方法先被调用"
tmp = super(Foo,cls).__new__(cls,*args, **kwargs)
print id(tmp)
print type(tmp)
print isinstance(tmp,Foo)
print issubclass(type(tmp),Foo)
return tmp
def __init__(self):
"""self是python默认传的值,该值是调用__new__的返回值"""
print "__init__被调用"
print id(self)
p = Foo()
print id(p)
print type(p)