Can I remove an inherited nested class from a python class?
-
30-06-2021 - |
Question
E.g. is this possible?
class Foo(object):
class Meta:
pass
class Bar(Foo):
def __init__(self):
# remove the Meta class here?
super(Bar, self).__init__()
Solution
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__()
OTHER TIPS
You can do it at the class level:
class Bar(Foo):
Meta = None
(also super
-calling the constructor is redundant)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow