Function attributes

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Feb 10 15:36:32 CET 2010


On Wed, 10 Feb 2010 05:59:41 -0800, Muhammad Alkarouri wrote:

> Hi everyone,
> 
> What is the simplest way to access the attributes of a function from
> inside it, other than using its explicit name? In a function like f
> below:
> 
> def f(*args):
>     f.args = args
>     print args
> 
> is there any other way?

Not built-in.


> I am guessing the next question will be: should I really care? It just
> feels like there should be a way, but I am not able to verbalise a valid
> one at the moment, sorry.

I completely agree with you. It is a wart that functions are only able to 
refer to themselves by name, because if the name changes, things break. 
Consider:

old_f = f  # save the old version of the function

def f(x):
    return old_f(x+1)  # make a new function and call it f

This won't work correctly, because old_f still tries to refer to itself 
under the name "f", and things break very quickly.





-- 
Steven



More information about the Python-list mailing list