Python 访问抽象/基类中尚未实现的属性的语法,无需实例化
我想定义一个基类,该基类实现一个静态方法,该方法访问要在继承基类的子类中实现的变量(试着不呼吸地大声说出来) 我如何在不依赖类实例化和使用Python 访问抽象/基类中尚未实现的属性的语法,无需实例化,python,python-3.x,oop,inheritance,abstract-class,Python,Python 3.x,Oop,Inheritance,Abstract Class,我想定义一个基类,该基类实现一个静态方法,该方法访问要在继承基类的子类中实现的变量(试着不呼吸地大声说出来) 我如何在不依赖类实例化和使用self的情况下(在Python3中)实现这一点,或者这是不可能的 class BaseClass: #This variable will be implemented in classes that inherit from this base class parameters = ... @staticmethod def d
self
的情况下(在Python3中)实现这一点,或者这是不可能的
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();