我可以从python类中删除继承的嵌套类吗?
这可能吗我可以从python类中删除继承的嵌套类吗?,python,inheritance,class-variables,Python,Inheritance,Class Variables,这可能吗 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):
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__()
您可以在班级级别执行此操作:
class Bar(Foo):
Meta = None
(同样
super
-调用构造函数是多余的)“超级调用构造函数是多余的”?父级的\uuuu init\uuuu
不会自动调用。如果你想调用父类的\uuuuu init\uuuu
,你必须显式地这样做。在他的例子中,他的\uuuuuu init\uuuu
只调用超类的\uuuu init\uuuuuu
,如果子类中没有定义\uuuuuu init\uuuu
,这是自动行为(如果我正确解释了你说的话)