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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:54:22+00:00 2026-05-16T02:54:22+00:00

I am trying to determine the owning class of a calling function in python.

  • 0

I am trying to determine the owning class of a calling function in python. For example, I have two classes, ClassA and ClassB. I want to know when classb_instance.call_class_a_method() is the caller of class_a.class_a_method() such that:

class ClassA(object):
    def class_a_method(self):
        # Some unknown process would occur here to
        # define caller. 
        if caller.__class__ == ClassB:
            print 'ClassB is calling.'
        else:
            print 'ClassB is not calling.'

class ClassB(object):
    def __init__(self):
        self.class_a_instance = ClassA()
    def call_class_a_method(self):
        self.class_a_instance.class_a_method()

classa_instance = ClassA()
classa_instance.class_a_method()
classb_instance = ClassB()
classb_instance.call_class_a_method()

the output would be:

'ClassB is not calling.'
'ClassB is calling.'

It seems as though inspect should be able to do this, but I cant puzzle out how.

  • 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-16T02:54:22+00:00Added an answer on May 16, 2026 at 2:54 am

    A function does not “have” a class — one or more classes may (but need not) refer to a given function object, e.g.:

    def f(self): return g()
    
    class One(object): bah = f
    
    class Two(object): blup = f
    
    def g(): ...
    

    One.bah and Two.blup are both set as references to the same function object f (and turn into unbound or bound method objects when accessed on the classes or their instances, but that leaves no trace in the stack). So, the stack upon

    One().f()
    

    vs

    Two().f()
    

    as seen from the g() that’s called in either case, is pretty hard indeed to distinguish — as would be that resulting from, e.g.,

    f('blah bloh blup')
    

    with no “classes” involved at all (though the type of the string being used as f‘s self argument would be;-).

    I recommend not relying on introspection — especially the wild and wooly kind, with lots of heuristics, that would be needed to get any semi-useful info here — for any production code requirements: rearchitect to avoid that need.

    For purely debugging purposes, in this case, you could walk the stack until you find a calling function with a first argument named self and introspect the type of the value bound to that argument name — but as I mentioned that’s entirely heuristical, since nothing forces your callers to name their first argument self if and only if their functions are meant to be methods in some class.

    Such heuristic introspection would produce type objects One, Two, and str, in the three examples of code I just gave — as you see, definitely not good for much else than debugging;-).

    If you clarify better exactly what you’re trying to accomplish through this attempted introspection, we might of course be able to help you better.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer On a POSIX system, you can use fileno() to convert… May 16, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer First up, you're using ID attributes incorrectly. According to the… May 16, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer You can use SVN hooks from command line, on various… May 16, 2026 at 8:34 am

Trending Tags

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

Top Members

Related Questions

No related questions found

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.