一般父类使用self.__class__
去调用某一个属性或者方法, 简单的理解就是调用它子类的方法和属性.
class Foo(object):
def create_new(self):
return self.__class__()
def create_new2(self):
return Foo()
class Bar(Foo):
pass
b = Bar()
c = b.create_new()
print type(c) # We got an instance of Bar
d = b.create_new2()
print type(d) # we got an instance of Foo
网友评论