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 744159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:56:46+00:00 2026-05-14T08:56:46+00:00

Trying to convert super(B, self).method() into a simple nice bubble() call. Did it ,

  • 0

Trying to convert super(B, self).method() into a simple nice bubble() call.
Did it, see below!

Is it possible to get reference to class B in this example?

class A(object): pass

class B(A):
    def test(self):
        test2()

class C(B): pass

import inspect
def test2():
    frame = inspect.currentframe().f_back
    cls = frame.[?something here?]
    # cls here should == B (class)

c = C()
c.test()

Basically, C is child of B, B is child of A. Then we create c of type C. Then the call to c.test() actually calls B.test() (via inheritance), which calls to test2().

test2() can get the parent frame frame; code reference to method via frame.f_code;
self via frame.f_locals['self']; but type(frame.f_locals['self']) is C (of course), but not B, where method is defined.

Any way to get B?

  • 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-14T08:56:46+00:00Added an answer on May 14, 2026 at 8:56 am

    Found a shorter way to do super(B, self).test() -> bubble() from below.

    (Works with multiple inheritance, doesn’t require arguments, correcly behaves with sub-classes)

    The solution was to use inspect.getmro(type(back_self)) (where back_self is a self from callee), then iterating it as cls with method_name in cls.__dict__ and verifying that the code reference we have is the one in this class (realized in find_class_by_code_object(self) nested function).

    bubble() can be easily extended with *args, **kwargs.

    import inspect
    def bubble(*args, **kwargs):
        def find_class_by_code_object(back_self, method_name, code):
            for cls in inspect.getmro(type(back_self)):
                if method_name in cls.__dict__:
                    method_fun = getattr(cls, method_name)
                    if method_fun.im_func.func_code is code:
                        return cls
    
        frame = inspect.currentframe().f_back
        back_self = frame.f_locals['self']
        method_name = frame.f_code.co_name
    
        for _ in xrange(5):
            code = frame.f_code
            cls = find_class_by_code_object(back_self, method_name, code)
            if cls:
                super_ = super(cls, back_self)
                return getattr(super_, method_name)(*args, **kwargs)
            try:
                frame = frame.f_back
            except:
                return
    
    
    
    class A(object):
        def test(self):
            print "A.test()"
    
    class B(A):
        def test(self):
            # instead of "super(B, self).test()" we can do
            bubble()
    
    class C(B):
        pass
    
    c = C()
    c.test() # works!
    
    b = B()
    b.test() # works!
    

    If anyone has a better idea, let’s hear it.

    Known bug: (thanks doublep) If C.test = B.test –> “infinite” recursion. Although that seems un-realistic for child class to actually have a method, that has been =‘ed from parent’s one.

    Known bug2: (thanks doublep) Decorated methods won’t work (probably unfixable, since decorator returns a closure)… Fixed decorator proble with for _ in xrange(5): … frame = frame.f_back – will handle up to 5 decorators, increase if needed. I love Python!

    Performance is 5 times worse than super() call, but we are talking about 200K calls vs a million calls per second, if this isn’t in your tightest loops – no reason to worry.

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

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As far as I can see from your code, you've… May 14, 2026 at 6:24 pm
  • Editorial Team
    Editorial Team added an answer DateTime startDt = new DateTime(2009, 1, 1); DateTime endDt =… May 14, 2026 at 6:24 pm
  • Editorial Team
    Editorial Team added an answer I've managed to work out a solution, not sure if… May 14, 2026 at 6:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.