python __init__ method in inherited class

前端 未结 6 1832
深忆病人
深忆病人 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:24

    Just call the parent's __init__ using super:

    class inheritedclass(initialclass):
        def __new__(self):
            self.attr3 = 'three'
            super(initialclass, self).__init__()
    

    I strongly advise to follow Python's naming conventions and start a class with a Capital letter, e.g. InheritedClass and InitialClass. This helps quickly distinguish classes from methods and variables.

提交回复
热议问题