【问题标题】:How to write the init for the child class of this class?这个类的子类的init怎么写?
【发布时间】:2019-05-28 10:11:29
【问题描述】:

我正在尝试从这个类编写一个子类:

class InsertCreateAlter(object):

def __init__(self, url=None, engine=None, connection=None, session=None,
             **kwargs):
    if connection is not None:
        self.engine = connection
    elif engine is not None:
        self.engine = engine
    elif session is not None:
        self.engine = session
    else:
        self.engine = sqlalchemy.create_engine(url, **kwargs)

这是我迄今为止的尝试:

class InsertCreateAlterEAV(InsertCreateAlter):


def __init__(self, url=None, engine=None, connection=None, session=None,
             **kwargs):
    InsertCreateAlter.__init__(self, url=None, engine=None, connection=None, session=None,
             **kwargs)
    if connection is not None:
        self.engine = connection
    elif engine is not None:
        self.engine = engine
    elif session is not None:
        self.engine = session
    else:
        self.engine = sqlalchemy.create_engine(url, **kwargs)

当我调用函数时:

InsertCreateAlterEAV(session=session)

我得到了错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-ee54fec26f76>", line 2, in <module>
    InsertCreateAlterEAV(session=session)
  File "/Users/asportelli/Documents/gitRepos/etl/etl/eav/InsertCreateAlterEAV.py", line 18, in __init__
    **kwargs)
  File "/Users/asportelli/Documents/gitRepos/etl/etl/insert/InsertCreateAlter.py", line 27, in __init__
    self.engine = sqlalchemy.create_engine(url, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 425, in create_engine
    return strategy.create(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 52, in create
    plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

【问题讨论】:

  • 你试过super()吗?
  • 你怎么称呼这个?
  • @shaikmoed 是的,也没有快乐
  • @doctorlove 我编辑它的问题
  • 为什么 engine 默认为 None ?另外,您为什么要从父类调用__init__() 而不是正常构造它?即使您将engine 的其他内容提供给您的子类,您对父类的__init__() 调用也会将其提供给None。您也不会将此 __init__() 调用的结果分配给任何内容,因此它会立即丢失。

标签: python class oop inheritance


【解决方案1】:

你不应该重复父类的代码。这样做:

class InsertCreateAlterEAV(InsertCreateAlter):
    def __init__(self, url=None, engine=None, connection=None, session=None,
                 **kwargs):
        # Do you have code you want to run before running the parent's __init__?
        # Maybe adding default arguments or changing one of them?
        # Then add that code here.

        # init the parent
        super().__init__(url=url,
                         engine=engine,
                         connection=connection,
                         session=session,
                         **kwargs)

        # Here you can add code after the parent's init code. Maybe for
        # initializing subclass-specific attributes.

如果您仍在使用 Python 2,请使用 InsertCreateAlter.__init__(self, ...


现在让我们看看问题出在哪里。你调用你的子类如下:

InsertCreateAlterEAV(session=session)

然后您的子类调用父类的__init__ 方法,但使用session=None。因此,父类采用了这个分支:

else:
        self.engine = sqlalchemy.create_engine(url, **kwargs)

因为kwargs 是空的,所以只有url(即None)被传递给create_engine()。 This method does not know how to deal with url==None.

因此,您会遇到异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2012-09-09
    相关资源
    最近更新 更多