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 Answer 1
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
lang-py
def
defines functions, not classes anddoStuff
isn't even inside aclass
.