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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:16:14+00:00 2026-05-15T17:16:14+00:00

is it possible to access the python function object attributes from within the function

  • 0

is it possible to access the python function object attributes from within the function scope?

e.g. let’s have

def f():
    return SOMETHING

f._x = "foo"
f()           # -> "foo"

now, what SOMETHING has to be, if we want to have the _x attribute content “foo” returned? if it’s even possible (simply)

thanks

UPDATE:

i’d like the following work also:

g = f
del f
g()          # -> "foo"

UPDATE 2:

Statement that it is not possible (if it is the case), and why, is more satisfying than providing a way how to fake it e.g. with a different object than a function

  • 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-15T17:16:15+00:00Added an answer on May 15, 2026 at 5:16 pm

    Solution

    Make one of the function’s default arguments be a reference to the function itself.

    def f(self):
        return self.x
    f.func_defaults = (f,)
    

    Example usage:

    >>> f.x = 17
    >>> b = f
    >>> del f
    >>> b()
    17
    

    Explanation

    The original poster wanted a solution that does not require a global name lookup. The simple solution

    def f():
        return f.x
    

    performs a lookup of the global variable f on each call, which does not meet the requirements. If f is deleted, then the function fails. The more complicated inspect proposal fails in the same way.

    What we want is to perform early binding and store the bound reference within the object itself. The following is conceptually what we are doing:

    def f(self=f):
        return self.x
    

    In the above, self is a local variable, so no global lookup is performed. However, we can’t write the code as-is, because f is not yet defined when we try to bind the default value of self to it. Instead, we set the default value after f is defined.

    Decorator

    Here’s a simple decorator to do this for you. Note that the self argument must come last, unlike methods, where self comes first. This also means that you must give a default value if any of your other arguments take a default value.

    def self_reference(f):
        f.func_defaults = f.func_defaults[:-1] + (f,)
        return f
    
    @self_reference
    def foo(verb, adverb='swiftly', self=None):
        return '%s %s %s' % (self.subject, verb, adverb)
    

    Example:

    >>> foo.subject = 'Fred'
    >>> bar = foo
    >>> del foo
    >>> bar('runs')
    'Fred runs swiftly'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Calling a function from a string with the function's name in Python
In Python, is it possible to get the object, say Foo , that contains
Is it possible to access arguments name strings?! function myFunction (a, b, c, d,
Is it possible to access the Data Purge Tool from the Valence API?
in python, how can i access the variables of one function into another function,
I need to access the elapsed time since startup in nanoseconds from a Python
I have an embedded device with Python installed on in. Is it possible to
I have to port an algorithm from an Excel sheet to python code but
Say, I have some scope with variables, and a function called in this scope
Using Python, is it possible to get access to the Most Recently Used (MRU)

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.