gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:48:35 31 4
gpt4 key购买 nike

例如这可能吗?

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 - 我可以从 python 类中删除继承的嵌套类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315442/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com