2014-06-08 15:28:44 -04:00
|
|
|
# Calling an inherited classmethod
|
|
|
|
class Base:
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def foo(cls):
|
|
|
|
print(cls.__name__)
|
|
|
|
|
2017-02-15 10:11:16 -05:00
|
|
|
try:
|
|
|
|
Base.__name__
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:03:01 -04:00
|
|
|
raise SystemExit
|
2017-02-15 10:11:16 -05:00
|
|
|
|
2014-06-08 15:28:44 -04:00
|
|
|
class Sub(Base):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
Sub.foo()
|
2015-12-09 12:30:01 -05:00
|
|
|
|
|
|
|
# overriding a member and accessing it via a classmethod
|
|
|
|
|
|
|
|
class A(object):
|
|
|
|
foo = 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def bar(cls):
|
|
|
|
print(cls.foo)
|
|
|
|
|
|
|
|
def baz(self):
|
|
|
|
print(self.foo)
|
|
|
|
|
|
|
|
class B(A):
|
|
|
|
foo = 1
|
|
|
|
|
|
|
|
B.bar() # class calling classmethod
|
|
|
|
B().bar() # instance calling classmethod
|
|
|
|
B().baz() # instance calling normal method
|