[英]Superclass __init__ overrides subclass constructor
我正在使用 cantera,嘗試通過創建子類Flow
來創建cantera.Solution
類的擴展。 這基本上是擴展氣相及其運動參數的描述。
Solution
是通過使用某些**kwargs
構建的。 我想要做的是將那些**kwargs
在Template
類對象中,並使用該對象來定義我的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.