编程语言
首页 > 编程语言> > 我可以从python类中删除一个继承的嵌套类吗?

我可以从python类中删除一个继承的嵌套类吗?

作者:互联网

例如.这可能吗?

class Foo(object):
    class Meta:
        pass

class Bar(Foo):
    def __init__(self):
        # remove the Meta class here?
        super(Bar, self).__init__()

解决方法:

您不能从继承的基类中删除类属性;您只能通过设置具有相同名称的实例变量来屏蔽它们:

class Bar(Foo):
    def __init__(self):
        self.Meta = None  # Set a new instance variable with the same name
        super(Bar, self).__init__()

你自己的类当然也可以用类变量覆盖它:

class Bar(Foo):
    Meta = None

    def __init__(self):
        # Meta is None for *all* instances of Bar.
        super(Bar, self).__init__()

标签:python,inheritance,class-variables
来源: https://codeday.me/bug/20190620/1248027.html