简体   繁体   中英

Calling a superclass method from a subclass in python

I was wondering if a subclass could call its parent static method, and tested it : it works like I hoped!

class A(object):
    @classmethod
    def static(cls):
    print('act on '+cls.__name__)

class B(A):
    def foo(self):
        print('foo()')

>>> B.static()
act on B

I wanted to know if it had pitfalls to be aware of when using that technique ...

Any advice?

What you are calling is the parent's classmethod , not a staticmethod .

Anyway, both of them are OK and common to be called with a subclass.

In case you override A 's classmethod in your B , you can still refer to A.static using super(B, cls).static .

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