Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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
Python 将值传递给类_Python_Oop - Fatal编程技术网

Python 将值传递给类

Python 将值传递给类,python,oop,Python,Oop,我有一个Python抽象类: class TransactionIdsGenerator(object): def getId(self): raise NotImplementedError 这个类实现了: class TransactionIdsGeneratorGeneric(TransactionIdsGenerator): INI_FILE = '/platpy/inifiles/postgres_config.ini' __dbManager =

我有一个Python抽象类:

class TransactionIdsGenerator(object):

  def getId(self):
      raise NotImplementedError
这个类实现了:

class TransactionIdsGeneratorGeneric(TransactionIdsGenerator):

  INI_FILE = '/platpy/inifiles/postgres_config.ini'    
  __dbManager = None

  def __init__(self):
     TransactionIdsGenerator.__init__(self)

  def getId(self):
     _ret = None
     _oDbManager = self.__getDbManager()
     if _oDbManager.execQuery("select nextval('send_99_seq');"):
         _row = _oDbManager.fetchOne()
         if _row is not None:
             _ret = _row[0]
     return _ret

  def __getDbManager(self):
     if self.__dbManager is None:
        self.__dbManager = PostgresManager(iniFile=self.INI_FILE)

     return self.__dbManager
在另一个文件中,我有这个类的实例:

  def __getTransactionIdsGenerator(self, operatorId):
      _ret = TransactionIdsGeneratorGeneric()
      return _ret
def __getTransactionIdsGenerator(self, operatorId):
  _ret = TransactionIdsGeneratorGeneric(operatorId)
  return _ret
是将varibale运算符ID传递给实例的某种方法,以便我可以在类中的方法getId中使用


谢谢

只需将其作为参数传递给
\uuuu init\uuu
。(请注意,在当前代码中,您甚至不需要定义
TransactionIdsGeneratorGeneric.\uuuu init\uuuu
,因为它所做的唯一事情就是调用父级的
\uu init\uuu

然后,在实例化该类时:

  def __getTransactionIdsGenerator(self, operatorId):
      _ret = TransactionIdsGeneratorGeneric()
      return _ret
def __getTransactionIdsGenerator(self, operatorId):
  _ret = TransactionIdsGeneratorGeneric(operatorId)
  return _ret

关键是子类的
\uuuu init\uuu
不需要与父类具有完全相同的签名,只要在调用它时确保将正确的参数传递给父类。如果您使用的是
super
,这并不完全正确,但既然您不是,我就忽略这个问题。:)

顺便说一句,你可能不需要所有这些丑陋的双下划线名称。。。