Function attributes again 
Author Message
 Function attributes again

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

        return z + a + b

Quote:
>>> f(8,3)

Traceback (most recent call last):
  File "<pyshell#39>", line 1, in ?
    f(8,3)
  File "<pyshell#28>", line 3, in f
    if __dict__ == None:
NameError: global name '__dict__' is not defined

What's going on? O:-)

'Fernando



Sun, 16 Nov 2003 22:37:20 GMT  
 Function attributes again

Quote:
> 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.

Quote:
>>> def f(a,b):

...   z=0
...   if not f.__dict__:
...     f.c = 4
...

Quote:
>>> f(8,3)
>>> f.c
4

Alex


Sun, 16 Nov 2003 23:08:23 GMT  
 Function attributes again

Quote:



>> 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:

Thanks! :-)

'Fernando



Mon, 17 Nov 2003 00:15:06 GMT  
 Function attributes again


Quote:
> 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

>    return z + a + b

>>>> f(8,3)
> Traceback (most recent call last):
>   File "<pyshell#39>", line 1, in ?
>     f(8,3)
>   File "<pyshell#28>", line 3, in f
>     if __dict__ == None:
> NameError: global name '__dict__' is not defined

> What's going on? O:-)

You're trying to access __dict__ as a global variable, but you probably
want to access your functions func_dict.

Quote:
>>> def f():

...   print f.x
...
Quote:
>>> f.x=31426
>>> f.func_dict

{'x': 31426}

/Fredrik

hint: dir is handy if you want to know what attributes
and functions a class, object or function etc. have.

Quote:
>>> dir(f)

['__dict__', '__doc__', '__name__', 'func_closure', 'func_code',
'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name',
'x']


Mon, 17 Nov 2003 00:12:42 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. counter function : How works the Set attribute function

2. function attributes are like function objects

3. Functions attributes accessed from within the function definition URGENT please

4. Scheme vs ML again and again and again and again

5. Scheme vs ML again and again and again and again

6. Newbie again: computing attributes on the fly

7. Deferred feature a function, an attribute ?

8. Allow attributes to be redefined as functions

9. Attribute to function

10. Bus resolution functions and Quiet attribute...

11. Dictionary attributes and functions

12. . Python 2.1 function attributes

 

 
Powered by phpBB® Forum Software