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

  • Home
  • SEARCH
  • 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 1114205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:58:23+00:00 2026-05-17T02:58:23+00:00

I’m trying to call a method of the superclass by using a variable method

  • 0

I’m trying to call a method of the superclass by using a variable method name. Normally, I would see the following two lines of code as equivalent:

someObj.method()
someObj.__getattribute__( 'method' )()

And in fact I believe, this is also what actually happens when I use the first line. However, in the following example, the second line produces a weird problem.

I use super to construct a super object and call the method of the super class. Doing it directly works as expected, but using __getattribute__ to get the method first, results in a indefinite loop which calls the method of the subclass again and again.

See the following code:

class A:
    def example ( self ):
        print( 'example in A' )

class B ( A ):
    def example ( self ):
        print( super( B, self ).example )
        print( super( B, self ).__getattribute__( 'example' ) )
        super( B, self ).example()
        #super( B, self ).__getattribute__( 'example' )()

        print( 'example in B' )

x = B()
x.example()

If you run that code, everything works as expected, and you should get an output similar to this:

<bound method B.example of <__main__.B object at 0x01CF6C90>>
<bound method B.example of <__main__.B object at 0x01CF6C90>>
example in A
example in B

So both methods, the one with the direct access and the one via __getattribute__, look identical. If you however replace the method call by the commented-out line, you’ll end up with a recursion runtime error.

Why does this happen, and more importantly, how can I actually access the method in the same way as python internally does, when I use the working line?

edit

When I thought I already tried everything, I found this to be working:

super.__getattribute__( super( B, self ), 'example' )()

It actually equals to super( B, self ).example.

  • 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-17T02:58:24+00:00Added an answer on May 17, 2026 at 2:58 am

    Don’t use __getattribute__ for this: it does not do what you think it does. (It is a specialized part of Python’s machinery, mainly for use if you’re implementing new attribute access magic.)

    For normal attribute access, use the getattr / setattr / delattr builtins:

    self.example           == getattr(self, 'example')
    super(B, self).example == getattr(super(B, self), 'example')
    

    (If you want to understand what __getattribute__ does, read the Descriptor HowTo Guide and Python Data model reference.)

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6

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.