美文网首页
python self.__class__的用法

python self.__class__的用法

作者: 南国青天 | 来源:发表于2015-12-18 19:57 被阅读1537次

一般父类使用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

相关文章

网友评论

      本文标题:python self.__class__的用法

      本文链接:https://www.haomeiwen.com/subject/nyiphttx.html