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)

?

Pang
10.2k146 gold badges87 silver badges126 bronze badges
asked May 8, 2017 at 10:39
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
answered May 8, 2017 at 10:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.