python-2.1 function attributes
Tim Peters
tim.one at home.com
Sat Jan 27 19:39:22 EST 2001
[Aahz Maruch]
> One thing I'm not clear on from all this discussion is whether one
> can do this:
>
> def f():
> f.a = 1
Sure, but there's no magic here:
1. f.a won't exist before you execute f() for the first time, so
I wouldn't *recommend* it.
2. The name "f" is not in f's local namespace (not in 2.1, or in
1.0 -- it's never been, and nothing has changed there), so this
does not "work":
class F:
def f(self):
f.a = 1
In the absence of any other "f" in the global namespace, executing F().f()
will yield something like
NameError: global name 'f' is not defined
when it gets to the "f.a = 1".
Note that these are nothing new, they're siblings of the old "why can't I
write a nested function/method that invokes itself recursively?" FAQ.
The intended way to write those:
def f():
xxx
f.a = 1
class F:
def f(self):
xxx
f.a = 1
More information about the Python-list
mailing list