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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:44:19+00:00 2026-05-16T14:44:19+00:00

Hope the title isn’t to confusing wasn’t sure how I should put it. I

  • 0

Hope the title isn’t to confusing wasn’t sure how I should put it. I wonder if it’s possible for the base class to know which method of the derived class called one of it’s methods.

Example:

class Controller(object):
    def __init__(self):
        self.output = {}

    def output(self, s):
        method_that_called_me = #is it possible?
        self.output[method_that_called_me] = s

class Public(Controller):
    def about_us(self):
        self.output('Damn good coffee!')

    def contact(self):
        self.output('contact me')

So is it possible for the output method to know which method from the Public class called it?

  • 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-16T14:44:19+00:00Added an answer on May 16, 2026 at 2:44 pm

    There is a somewhat magical way to do what you are looking for using introspection on the call stack. But that isn’t portable since not all implementations of Python have the necessary functions. It’s probably not a good design decision to use introspection either.

    Better, I think, to be explicit:

    class Controller(object):
        def __init__(self):
            self._output = {}
    
        def output(self, s, caller):
            method_that_called_me = caller.__name__
            self._output[method_that_called_me] = s
    
    class Public(Controller):
        def about_us(self):
            self.output('Damn good coffee!',self.about_us)
    
        def contact(self):
            self.output('contact me',self.contact)
    

    PS. Note that you have self.output as both a dict and a method. I’ve altered it so self._output is a dict, and self.output is the method.

    PPS. Just to show you what I was referring to by magical introspection:

    import traceback
    
    class Controller(object):
        def output_method(self, s):
            (filename,line_number,function_name,text)=traceback.extract_stack()[-2]
            method_that_called_me = function_name
            self.output[method_that_called_me] = s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.