0

I have a 'static' class

class A:
    a = 1

    @staticmethod
    def doStuff():
        foo(A.a)

Now I need a derived class

class B(A):
    a = 2

that basically does

    @staticmethod
    def doStuff():
        foo(B.a)

If A would not be a pseudo static class, I could just derive B from A and

foo(self.a)

would do what I want. Is there a way to avoid copying doStuff() into class B and replace foo(A.a) with foo(B.a)? Something along the line of referring to the class in a 'self' way and having class A s doStuff look like

def doStuff():
    foo(class_self.a)

?

1
  • Does your code even run? Where are your classes? def defines functions, not classes and doStuff isn't even inside a class. Commented May 8, 2017 at 10:41

1 Answer 1

2

I assume you mean class instead of def in your code.

The answer is not to use a staticmethod, but a classmethod. This would behave exactly as you want.

class A:
    a = 1

    @classmethod
    def doStuff(cls):
        foo(cls.a)

class B(A):
    a = 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.