1

I have the following two classes A and B. How do I make the do_someting() method call the overriden method, some_method(), in B. Is this doable in Python?

class A:
    @staticmethod
    def some_method()
        # pass
        return

    @classmethod
    def do_something():
        A.some_method()
        ...
        return

class B(A):
    @staticmethod
    def some_method()
        # how does do_something call here?
        return

    @classmethod
    def run()
        B.do_something()
        return

1 Answer 1

1

It's pretty simple, just make sure to fix your colons pass in self and cls:

class A:
    @staticmethod
    def some_method():
        # pass
        return

    @classmethod
    def do_something(cls):
        cls.some_method()
        return

class B(A):
    @staticmethod
    def some_method():
        print("I did stuff!")
        return

    @classmethod
    def run(cls):
        B.do_something()
        return

k = B()
k.run()
>>>"I did stuff!"

And if you want to call the old do_something (the one in class A) from class B, just pass in the appropriate class. In class B:

@classmethod
def run(cls):
    A.do_something()
    return
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.