How to call a Parent Class's method from Child Class in Python?

后端 未结 15 2384
無奈伤痛
無奈伤痛 2020-11-22 10:14

When creating a simple object hierarchy in Python, I\'d like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for

相关标签:
15条回答
  • 2020-11-22 11:03

    In this example cafec_param is a base class (parent class) and abc is a child class. abc calls the AWC method in the base class.

    class cafec_param:
    
        def __init__(self,precip,pe,awc,nmonths):
    
            self.precip = precip
            self.pe = pe
            self.awc = awc
            self.nmonths = nmonths
    
        def AWC(self):
    
            if self.awc<254:
                Ss = self.awc
                Su = 0
                self.Ss=Ss
            else:
                Ss = 254; Su = self.awc-254
                self.Ss=Ss + Su   
            AWC = Ss + Su
            return self.Ss
    
    
        def test(self):
            return self.Ss
            #return self.Ss*4
    
    class abc(cafec_param):
        def rr(self):
            return self.AWC()
    
    
    ee=cafec_param('re',34,56,2)
    dd=abc('re',34,56,2)
    print(dd.rr())
    print(ee.AWC())
    print(ee.test())
    

    Output

    56
    
    56
    
    56
    
    0 讨论(0)
  • 2020-11-22 11:03

    In Python 2, I didn't have a lot luck with super(). I used the answer from jimifiki on this SO thread how to refer to a parent method in python?. Then, I added my own little twist to it, which I think is an improvement in usability (Especially if you have long class names).

    Define the base class in one module:

     # myA.py
    
    class A():     
        def foo( self ):
            print "foo"
    

    Then import the class into another modules as parent:

    # myB.py
    
    from myA import A as parent
    
    class B( parent ):
        def foo( self ):
            parent.foo( self )   # calls 'A.foo()'
    
    0 讨论(0)
  • 2020-11-22 11:05

    There is a super() in python also.

    Example for how a super class method is called from a sub class method

    class Dog(object):
        name = ''
        moves = []
    
        def __init__(self, name):
            self.name = name
    
        def moves_setup(self,x):
            self.moves.append('walk')
            self.moves.append('run')
            self.moves.append(x)
        def get_moves(self):
            return self.moves
    
    class Superdog(Dog):
    
        #Let's try to append new fly ability to our Superdog
        def moves_setup(self):
            #Set default moves by calling method of parent class
            super().moves_setup("hello world")
            self.moves.append('fly')
    dog = Superdog('Freddy')
    print (dog.name)
    dog.moves_setup()
    print (dog.get_moves()) 
    

    This example is similar to the one explained above.However there is one difference that super doesn't have any arguments passed to it.This above code is executable in python 3.4 version.

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