Я хочу определить базовый класс, который реализует метод stati c, который обращается к переменной, которая будет реализована в дочерних классах, которые наследуют базовый класс (попробуйте сказать это вслух, не дыша) .
class BaseClass:
#This variable will be implemented in classes that inherit from this base class
parameters = ...
@staticmethod
def do_something(**args):
#This loop below doesn't work because the `parameters` variable is not in the local scope
#However, I don't want to instantiate the class and access it through 'self' - I need this to be a static method.
for parameter in parameters:
print(parameter)
class ChildClass1(BaseClass):
#This child class implements the 'parameters' variable
parameters = ["p1", "p2", "p3"]
class ChildClass2(BaseClass):
#This child class implements the 'parameters' variable
parameters = ["a1", "b2", "c3"]
ChildClass1.do_something();
ChildClass2.do_something();
Как мне заставить это работать (в Python 3), не полагаясь на создание экземпляров класса и используя self
, или это невозможно?