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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:47:19+00:00 2026-05-20T07:47:19+00:00

I can access a python function’s attribute inside of function itself by below code:

  • 0

I can access a python function’s attribute inside of function itself by below code:

def aa():
    print aa.__name__
    print aa.__hash__
    # other simliar

However, if above aa() function is a template for write other code, say bb(), I have to write:

def bb():
    print bb.__name__
    print bb.__hash__
    # other simliar

Is there a “pointer” similar to the self argument in a class method so I could write code like this?

def whatever():
    print self.__name__
    print self.__hash__
    # other simliar

I searched and found someone said to use the class to solve this problem, but that may be a trouble to redefine all the existing functions. Any suggestions?

  • 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-20T07:47:19+00:00Added an answer on May 20, 2026 at 7:47 am

    There is no generic way for a function to refer to itself. Consider using a decorator instead. If all you want as you indicated was to print information about the function that can be done easily with a decorator:

    from functools import wraps
    def showinfo(f):
        @wraps(f)
        def wrapper(*args, **kwds):
             print(f.__name__, f.__hash__)
             return f(*args, **kwds)
        return wrapper
    
    @showinfo
    def aa():
        pass
    

    If you really do need to reference the function, then just add it to the function arguments:

    def withself(f):
        @wraps(f)
        def wrapper(*args, **kwds):
            return f(f, *args, **kwds)
        return wrapper
    
    @withself
    def aa(self):
          print(self.__name__)
          # etc.
    

    Edit to add alternate decorator:

    You can also write a simpler (and probably faster) decorator that will make the wrapped function work correctly with Python’s introspection:

    def bind(f):
        """Decorate function `f` to pass a reference to the function
        as the first argument"""
        return f.__get__(f, type(f))
    
    @bind
    def foo(self, x):
        "This is a bound function!"
        print(self, x)
    
    
    >>> foo(42)
    <function foo at 0x02A46030> 42
    >>> help(foo)
    Help on method foo in module __main__:
    
    foo(self, x) method of builtins.function instance
        This is a bound function!
    

    This leverages Python’s descriptor protocol: functions have a __get__ method that is used to create bound methods. The decorator simply uses the existing method to make the function a bound method of itself. It will only work for standalone functions, if you wanted a method to be able to reference itself you would have to do something more like the original solution.

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

Sidebar

Related Questions

in python, how can i access the variables of one function into another function,
Can I access a users microphone in Python? Sorry I forgot not everyone is
does anyone know where on access 2007 a function where i can access my
How can I get a variable that contains the currently executing function in Python?
Quick question: How can I access the BN_CLICKED constant and other constants defined for
I can access the database either from a .NET program (using ODBC) or through
I know you can access the Contact Store from the SDk, but is it
In PHP you can access characters of strings in a few different ways, one
Is there a way I can access (for printout) a list of sub +
Is there any way i can access the page object from within the global.asax

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.