python __init__ method in inherited class

前端 未结 6 1825
深忆病人
深忆病人 2021-02-07 01:36

I would like to give a daughter class some extra attributes without having to explicitly call a new method. So is there a way of giving the inherited class an __init__

6条回答
  •  迷失自我
    2021-02-07 02:19

    class initialclass:
            def __init__(self):
                    self.attr1 = 'one'
                    self.attr2 = 'two'
    class inheritedclass(initialclass):
            def __init__(self):
                    super().__init__()
                    self.attr3 = 'three'
            def somemethod(self):
                    print (self.attr1, self.attr2, self.attr3)
    a=inheritedclass()
    a.somemethod()
    
     1. List item
    

提交回复
热议问题