Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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
Class 以类似的方式初始化不同的类_Class_Object_Initialization_Python 3.5 - Fatal编程技术网

Class 以类似的方式初始化不同的类

Class 以类似的方式初始化不同的类,class,object,initialization,python-3.5,Class,Object,Initialization,Python 3.5,我有不同的类,我需要以几乎相同的方式部分初始化它们。init函数接收输入文件路径和/或一些numpy数据 class InertialData1: def __init__(self, file=None, data=None): # --- this is the (almost) common part --- if file is None: self.file = 'Unknown path to file' else:

我有不同的类,我需要以几乎相同的方式部分初始化它们。init函数接收输入文件路径和/或一些numpy数据

class InertialData1:
   def __init__(self, file=None, data=None):
      # --- this is the (almost) common part ---
      if file is None:
        self.file = 'Unknown path to file'
      else:
        self.file = file
      assert isinstance(self.file, str)

      if data is None:
        self.data = read_logfile(file, 'a_token')
      else:
        self.data = data
      assert isinstance(self.data, np.ndarray)
      # --- end of the common part ---

      # Here other stuff different from class to class
外部函数read_logfile函数中的令牌也会随着类的不同而变化

class InertialData2:
   def __init__(self, file=None, data=None):
      # here the common code as above, but with 'another_token' instead of 'a_token'
      # here other stuff
...
当然,在所有的类定义中写相同的行是不可能的

可能的解决办法

我想定义一个这样的外部函数

def initialize(file, data, token):
    if file is None:
        file = 'Unknown path to file'
    else:
        file = file
    assert isinstance(file, str)

    if data is None:
        data = read_logfile(file, token)
    else:
        data = data
    assert isinstance(data, np.ndarray)
return file, data
然后我可以在每个类的init方法中使用它,如下所示:

class InertialData1:
   def __init__(self, file=None, data=None):
        self.file = file
        self.data = data
        self.file, self.data = initialize(self.file, self.data, token='a_token')
这种方法似乎奏效了。我只是觉得这不是最好的方法,或者不是pythonic,我希望从你的回答中学到一些东西:
谢谢

您应该能够通过使用类继承来干燥代码,而不是重复代码:

class InertialData:
  def __init__(self, file=None, data=None):
    # --- this is the common part ---
    if file is None:
      self.file = 'Unknown path to file'
    else:
      self.file = file
    assert isinstance(self.file, str)

  def init_data(self, token, data):
    if data is None:
      self.data = read_logfile(self.file, token)
    else:
      self.data = data
    assert isinstance(self.data, np.ndarray)

class InertialData1(InertialData):
  def __init__(self, file=None, data=None):
    # --- this is the different part ---
    super().__init__(file=file, data=data);
    self.init_data('token_a', data)
请注意以下几点:

您可以创建一个具有重复功能的公共类InertialData。然后,所有自定义类都可以从公共类继承。查看类InertialData1如何将InertialData作为参数

在从InertialData继承的所有类中,您都可以调用super,这将调用超类的_init___方法,在本例中为InertialData

初始化每个类具有不同令牌的数据的代码位已被拉入以令牌为参数的函数中。现在,您可以在继承自InertialData的任何类的uuu init_uuu函数中调用initialize_data'token_a'


我将您的类重命名为以大写字母开头,这是Python的惯例。

谢谢您的回复。然而,它似乎不是这样工作的。TypeError:super不接受关键字ArgumentsOrry,我忘了super会自动重用传入的参数。我还意识到您需要将数据传递到init_数据方法中,因此我补充说,代码toosuper本身也不起作用。超级。初始化文件,数据正在工作。但是,我不确定这样调用base init函数是否是一种好的做法。我现在明白我的错误了。我在用Ruby语言给super打电话。。。是的,在Python3中使用super.\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。见答案。不过,您仍然需要使用关键字参数。我相应地更新了我的答案。@Robyc,这个解决方案现在看起来更好了吗?