getattr() in default namespace.
Yermat
loic at fejoz.net
Thu Apr 8 07:49:15 EDT 2004
SimonVC wrote:
> Hi group, long time reader first time poster (ive always wanted to say
> that).
>
> How do you use getattr() to get a refernce to a function defined in
> the default namespace..
>
> eg:
>
>
>>>>dir()
>
> ['__doc__', '__name__', 'b1', 'b2', 'b3', 'rd']
>
>
>>>>b1
>
> <function b1 at 1876939>
>
>>>>getattr(self, "b1")
>
>
> Traceback (innermost last):
> File "<console>", line 1, in ?
> NameError: self
>
>
>>>>getattr("b1")
>
> Traceback (innermost last):
> File "<console>", line 1, in ?
> TypeError: getattr(): expected 2-3 args; got 1
>>> def b1(a):
... return a
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'b1']
>>> globals().keys()
['__builtins__', '__name__', '__doc__', 'b1']
>>>
>>> globals()['b1']
<function b1 at 0x402e241c>
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', '__doc__': None, 'b1': <function b1 at 0x402e241c>}
I can't see what you can do with getattr here...
but :
>>> len
<built-in function len>
>>> getattr(__builtins__,"len")
<built-in function len>
>>> getattr(__builtins__,"len") is len
True
Yermat
More information about the Python-list
mailing list