Class() vs self.__class__()

  1. 类的内部创建实例,这两种方法的区别

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

posted @ 2021-03-31 15:11  该显示昵称已被使用了  阅读(82)  评论(0编辑  收藏  举报