例如,这可能吗?
class Foo(object):
class Meta:
pass
class Bar(Foo):
def __init__(self):
# remove the Meta class here?
super(Bar, self).__init__()
例如,这可能吗?
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
- 调用构造函数是多余的)