3

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

1
  • 1
    You can't do that (without tricks). Why not just define it as a regular method at the class level? Commented Dec 2, 2011 at 14:32

2 Answers 2

4

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?

Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't necessarily disappear - this is a closure, and a common idiom in Python. If the returned function is not just invoked immediately, but saved as a variable in the caller, then it can be called and used again and again.
@PaulMcGuire: Point taken. I was talking about the specific code rather than in general. I'll edit the disappearance remark out.
In fact, I can't... The base class is not mine, so I'm not allow to change the method
2

This is called a closure (a function within a function) and does not quite behave like what you're asking. Because functions are not like classes, they do not have the concept of "instances", and therefore any objects internal to the function are not externally accessible unless they are returned by the function.

NB, In Python methods are essentially functions that are bound to a class.

This would be a better pattern:

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

    def submethod():
        return "Submethod action"

You may access internal state of closures, but it's not the same as attribute-style access you're thinking. It's better to avoid them until you fully understand how Python's namespaces and stuff and junk work.

3 Comments

Thank you for the explanation ! I think that this pattern is better but I'm not allow to change this...
Do you think there is an advantage to use the closure in this case ?
No there isn't in this case. It would be worth your while to study up on them to see if they are something you might consider in the future. :) BTW, if you're not allowed to change the method, you can subclass the base class and overload the method in the subclass. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.