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)
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.