简体   繁体   中英

Can I remove an inherited nested class from a python class?

Eg is this possible?

class Foo(object):
    class Meta:
        pass

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

You cannot remove class attributes from an inherited base class; you can only mask them, by setting an instance variable with the same name:

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

Your own class could of course also override it with a class variable:

class Bar(Foo):
    Meta = None

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

You can do it at the class level:

class Bar(Foo):
    Meta = None

(also super -calling the constructor is redundant)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM