getattr() in default namespace.

S

SimonVC

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:
['__doc__', '__name__', 'b1', 'b2', 'b3', 'rd']

Traceback (innermost last):
Traceback (innermost last):
File "<console>", line 1, in ?
TypeError: getattr(): expected 2-3 args; got 1
 
Y

Yermat

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


['__doc__', '__name__', 'b1', 'b2', 'b3', 'rd']



Traceback (innermost last):
File "<console>", line 1, in ?
NameError: self


Traceback (innermost last):
File "<console>", line 1, in ?
TypeError: getattr(): expected 2-3 args; got 1
.... return a
....
>>> dir() ['__builtins__', '__doc__', '__name__', 'b1']
>>> globals().keys() ['__builtins__', '__name__', '__doc__', 'b1']
>>>
>>> globals()['b1']
>>> 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 :True

Yermat
 
C

Carl Banks

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

It's not really possible to do this in general. I know of no way, for
example, to access the current namespace with getattr inside a class
definition.

However, I'm assuming you're taking about a module's namespace (i.e.,
a so-called global namespace) and not a class or function namespace.
You can do this very easily with "namespace = __import__(__name__)".
It even works at top level.


Python 2.3.2 (#2, Dec 17 2003, 13:21:01) [C] on hp-ux11
Type "help", "copyright", "credits" or "license" for more
information. 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top