Function attributes again

Alex Martelli aleaxit at yahoo.com
Wed May 30 17:08:23 CEST 2001


"Fernando Rodríguez" <spamers at must.die> wrote in message
news:h71ahtoc3gn0eft2se5obpoh5kpndqqbua at 4ax.com...
> Hi!
>
> How can I check (from within a function) if it has attributes?  I
> tried if __dict__ == None in the definition, but it doesn't work:
>
> def f(a, b):
> z = 0
> if __dict__ == None:
> __dict__['c'] = 4

You have to check for f.__dict__ instead:

D:\Python21>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> def f(a,b):
...   z=0
...   if not f.__dict__:
...     f.c = 4
...
>>> f(8,3)
>>> f.c
4
>>>


Alex






More information about the Python-list mailing list