[Zope] How to access the namespace of a Python Script?

Dieter Maurer dieter at handshake.de
Wed Oct 1 15:20:07 EDT 2003


Elena Schulz wrote at 2003-9-30 16:21 +0200:
 > I try to do the following inside a python script:
 > 
 > myDict={'myKey':'myVal'}
 > myString='myDict'
 > 
 > myObject = getattr(thisScriptNameSpace, myString)
 > 
 > What should I write for 'thisScriptNameSpace' to get 'myObject' pointing to
 > 'myDict'?

Please do not abuse the local namespace...

Use something else for this, a dictionary for example.

 > In a 'normal' Python-Modul 'thisScriptNameSpace' should be 'self'.

No. In a Python module, you would use "globals().get".
"globals()" gives you the module's dictionary.

 > It should be possible in a Python Script, ins't it?

A Python Script is a function. The builtin function
"locals()" returns the function's local namespace as a
dictionary. The effect of modifications to this dictionary
is undefined.

Almost surely, "locals" is not available in a Python Script.

You could also use "eval", but this, too, is not available
in a Python Script...


Thus, use a dictionary...


Dieter



More information about the Zope mailing list