Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a function access its own attributes?

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

like image 928
mykhal Avatar asked Jun 24 '10 10:06

mykhal


People also ask

Can a function have an attribute?

Functions are also objects. Therefore functions must also have assignable attributes.”

Can a function have attributes Python?

Currently, Python supports function attributes only on Python functions (i.e. those that are written in Python, not those that are built-in).

How do you define a function attribute?

A function attribute is specified with the keyword __attribute__ followed by the attribute name and any additional arguments the attribute name requires. A function __attribute__ specification is included in the declaration or definition of a function.


2 Answers

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' 
like image 117
Mark Lodato Avatar answered Sep 21 '22 23:09

Mark Lodato


You could just use a class to do this

>>> class F(object): ...     def __call__(self, *args, **kw): ...         return self._x ...  >>> f=F() >>> f._x = "foo" >>> f() 'foo' >>> g=f >>> del f >>> g() 'foo' 
like image 34
John La Rooy Avatar answered Sep 24 '22 23:09

John La Rooy