1
我有許多派生類的foo類,我試圖將基類的對象實例化爲派生類之一。我只想調用一次基類的構造函數。 我收到運行時錯誤當我嘗試執行下面的一段代碼:運行時錯誤:RecursionError:調用Python對象時超出最大遞歸深度
class foo():
call_once = True
_Bar = None
_BaseClass = None
def __init__(self):
if (self.call_once):
self.call_once = False
self._Bar = bar()
self._BaseClass = _bar
class bar(foo):
def __init__(self):
foo.__init__(self)
bar1 = bar()
這沒有任何意義。讓'foo'實例沒有被初始化有什麼意義?爲什麼第一個特別?無論你在做什麼,我保證有一個更好的方法來做到這一點。 –
請建議更好的方法。我寫了一個問題,我想要做什麼。 –
因此'foo()'實例化一個類型爲「bar」的對象。但是'bar()'調用'foo()'的'__init__'方法,然後實例化'bar'類型的對象。這導致無限循環。 'if'語句在這裏沒有幫助,因爲遞歸發生在'if'block中。 – MaxPowers