Invisible function attributes

sismex01 at hebmex.com sismex01 at hebmex.com
Wed Sep 3 11:22:46 EDT 2003


[lefevrol at yahoo.com]
>  
>  Python 2.3
>  >>> def foo():
>  ...     foo.a = 1
>  ...
>  >>> vars(foo)
>  {}
>  >>> foo()
>  >>> vars(foo)
>  {'a': 1}
>  >>>
>  
>  So it would appear that function attributes are not really
>  there until the first call to the function. If that is the
>  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,
>

not so, function attributes are just like normal object attributes:

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> def foo2():
	return 9.86960440366

>>> foo2.a = "x"
>>> foo2.a
'x'
>>> 

So... the only reason that your 'foo' got it's attribute until
you ran it, was because *inside* foo was the definition of said
attribute; you could always have defined the attribute outside
of foo anytime.

-gustavo



Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.





More information about the Python-list mailing list