Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6883173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:20:55+00:00 2026-05-27T05:20:55+00:00

I would like inherit a ‘submethod’ of a method in python. Could somebody help

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:20:55+00:00Added an answer on May 27, 2026 at 5:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a RESTful web service in python and would like to add some
I would like to do this: class Derived; class Base { virtual Derived f()
I have usercontrols which inherit a base class which looks like this: BasePage.cs: using
I would like to utilize classes that inherit the System.Data name space. Specifically, I
I would like class B to inherit the Javadoc from an interface that it
I would like to share a client singleton across GWT modules. Is this possible?
I have a generic repository and would like to implement a generic GetById method.
I've got a class that I would like to inherit. i.e. ExpenseForm should inherit
There is this Telerik's RadTreeView (Silverlight Version) control I'd like to extend (inherit from).
I would like to create a simple control that inherits from HeaderedContentControl, and has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.