python __init__ method in inherited class

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

    It's incredibly simple. Define a new __init__ method and call the parent's __init__ at the beginning.

    # assuming a class Base, its __init__ takes one parameter x
    
    class Derived(Base):
        def __init__(self, x, y):
            # whatever initialization is needed so we can say Derived is-a Base
            super(Derived, self).__init__(x)
            # now, add whatever makes Derived special - do your own initialization
            self.y = y
    

    In Python 3, you don't have to (and therefore propably shouldn't, for simplicity) explicitly inherit from object or pass the class and self to super.

    0 讨论(0)
  • 2021-02-07 02:07

    Just call a designated method from the parent's init, if it exists:

    class initialclass():
        def __init__(self):
            self.attr1 = 'one'
            self.attr2 = 'two'  
            if hasattr(self, 'init_subclass'):
                self.init_subclass()
    
    class inheritedclass(initialclass):
        def init_subclass(self):
            self.attr3 = 'three'
    
    0 讨论(0)
  • 2021-02-07 02:09

    First of all you're mixing __init__ and __new__, they are different things. __new__ doesn't take instance (self) as argument, it takes class (cls). As for the main part of your question, what you have to do is use super to invoke superclass' __init__.

    Your code should look like this:

    class initialclass(object):
        def __init__(self):
            self.attr1 = 'one'
            self.attr2 = 'two'    
    
    class inheritedclass(initialclass):
        def __init__(self):
            self.attr3 = 'three'
            super(inheritedclass, self).__init__()
    
    0 讨论(0)
  • 2021-02-07 02:16

    As far as I know that's not possible, however you can call the init method of the superclass, like this:

    class inheritedclass(initialclass):
        def __init__(self):
            initialclass.__init__(self)
            self.attr3 = 'three'
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题