Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问抽象/基类中尚未实现的属性的语法,无需实例化_Python_Python 3.x_Oop_Inheritance_Abstract Class - Fatal编程技术网

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();