Invisible function attributes

John J. Lee jjl at pobox.com
Wed Sep 3 19:16:01 EDT 2003


lefevrol at yahoo.com (Olivier Lefevre) writes:
[...]
> So it would appear that function attributes are not really
> there until the first call to the function. If that is the

Not function attributes in general, just those that are first assigned
to in the function body.  No special rule here, though, because...


> intended behaviour, it is really weird. I couldn't find any
> explicit discussion of this topic in the LRM.
> 
> Thanks if anyone can shed some light on this,

...what I'm guessing you haven't figured out yet is that everything
works like this in Python.  For example, what might be called a 'class
declaration' in other languages isn't really a declaration in Python,
it's code that gets executed at runtime.  Same is true of functions:

if WANT_SPAM:
    def sayhello(): print "spam"
else:
    def sayhello(): print "eggs"

sayhello()


And your foo.a = 1 isn't a declaration (Python doesn't have them,
really), it's just an attribute assignment.


John




More information about the Python-list mailing list