How do I basically do this?
class Parent:
Foo = 'Parent food'
@staticmethod
def bar():
# want to print whatever the child's Foo is
class Child(Parent):
Foo = 'Child foo'
# This should print "Child foo"
Child.bar()
You can use a classmethod for that
class Parent:
Foo = 'Parent food'
@classmethod
def bar(cls):
print cls.Foo
class Child(Parent):
Foo = 'Child foo'
Child.bar()
# This will print "Child foo"
As mentioned in the comments, what you need is a classmethod.
class Parent:
Foo = 'Parent food'
@classmethod
def bar(cls):
print(cls.Foo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With