簡體   English   中英

超類 __init__ 覆蓋子類構造函數

[英]Superclass __init__ overrides subclass constructor

我正在使用 cantera,嘗試通過創建子類Flow來創建cantera.Solution類的擴展。 這基本上是擴展氣相及其運動參數的描述。

Solution是通過使用某些**kwargs構建的。 我想要做的是將那些**kwargsTemplate類對象中,並使用該對象來定義我的Flow 我試圖用Flow.__init__遮蔽Solution.__init__並使用super()Template傳遞參數。 但是, Solution.__init__似乎覆蓋Flow.__init__

class Flow(Solution):

  def __init__(self, template, velocity):

    super().__init__(species = template.species,
                     reactions = template.reactions, 
                     thermo = template.thermo, 
                     kinetics = 'GasKinetics')

    self.velocity = velocity

現在,讓我們假設我有一個名為template的適當Template對象,它充當Solution.__init__所需的所有**kwargs的容器。 我正在嘗試創建我的Flow對象:

flow = Flow(template, 230)

我得到:

AttributeError: 'Template' object has no attribute 'encode'

如果我嘗試,我會得到同樣的錯誤:

S = Solution(template, 230)

所以基本上我所有的子類構造函數參數都傳遞給超類的構造函數。 由於未覆蓋超類構造函數,因此我無法使用Template對象將我的Solution定義為Flow的基礎。 我在網上讀到這不是默認行為,因為子類構造函數應該覆蓋超類構造函數。 如何幫助?

我找到了解決辦法。 事實證明,Cantera 是基於 cython 的。 事實上,解決方案類包含init ,但它不被 Python 視為一個函數,而是一個槽包裝器。 因此,子類無法使用自己的構造函數隱藏 init 我使用代碼中未提及的內容優雅地處理了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM