一般父類使用self.__class__去調(diào)用某一個(gè)屬性或者方法, 簡單的理解就是調(diào)用它子類的方法和屬性.
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