[Pycharm]スーパークラスの__init__へのプロンプト呼び出しがありません



Prompt Call __init__ Super Class Is Missed



このプロンプトは継承で表示されます。サブクラスが親クラスの__init __()メソッドを明示的に呼び出さない場合、プロンプトはサブクラスでプロンプトが表示されます。

親クラスの初期化メソッドを呼び出すことを忘れないようにユーザーに促すためだけに、コードにエラーはありません。



class Base: def __init__(self): print('base init') class Derived1(Base): # will be prompted on the init method here def __init__(self): print('derived1 init') class Derived2(Base): def __init__(self): super(Derived2, self).__init__() print('derived2 init') print('Creating Derived1...') d1 = Derived1() print('Creating Derived2...') d2 = Derived2()

親クラスのinitメソッドを継承し、それを次のコードに変更すると、プロンプトは表示されません。

class Parent(object): def __init__(self, name): self.name = name print('create an instance of:', self.__class__.__name__) print('name attribute is:', self.name) #Subclass inherits the parent class class Child(Parent): def __init__(self,name): print('call __init__ from Child class') super().__init__(name) #c = Child('init Child') #print() #Instantiate the subclass c = Child('Tom') print(c.name)