私はいくつかのクラスを持っています
class Parent():
def DoThing():
if isinstance(self, 'child1'):
DoSomething()
elif:
DoSomethingElse()
import Parent
class Child1(Parent):
def DoThing():
#Do some things here
super.DoThing()
import Parent
class Child2(Parent)
def DoThing():
#Do other things
super.DoThing()
私が抱えている問題は、クラスのインスタンスが親自体であるか、子の 1 つであるかを確認したいということです。
失敗のポイントは、親を解釈するときです。インタープリターは Child1 が何であるかを認識していないため、プロセスは失敗します。再帰が発生するため、 Child1 をインポートできません。
親と子1でメソッドを定義することにより、これを回避する方法を作成しました。
def IsChild1Instance(self):
return True/False
これを行うためのより良い、よりクリーンな方法はありますか?