Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit a sub method in Python

I would like inherit a 'submethod' of a method in python. Could somebody help me to figure out how to do this please ?

Example of what I want to do :

 class A(object):
    def method(self, val):
        def submethod():
            return "Submethod action"
        if not val:
            return submethod()  
        return "Method action"


a = A()

class B(A):
    def method(self, val):
        #inherit submethod ?        
        def submethod():
            return "Another submethod action"

        return super(B,self).method(val)

b = B()
print "A : "
print a.method(True)
>> Method action
print a.method(False)
>> Submethod action
print "B : "
print b.method(True)
>> Method Action
print b.method(False)
Actual answer : 
>> Submethod Action
**Wanted answer : 
>> Another submethod action**

Kind regards,

Quentin

like image 936
QGerome Avatar asked Aug 25 '26 13:08

QGerome


1 Answers

You can't "inherit" submethod since it's local to method and doesn't even exist until method is run.

Why not simply make it a top-level method of A and B?

like image 174
NPE Avatar answered Aug 27 '26 04:08

NPE