Adding an object to the global namespace through " f_globals" is that allowed ?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Jul 3 08:50:10 EDT 2009


Stef Mientki a écrit :
> hello,
> 
> I need to add an object's name to the global namespace.
> The reason for this is to create an environment,
> where you can add some kind of math environment,
> where no need for Python knowledge is needed.
> 
> The next statement works,
> but I'm not sure if it will have any dramatical side effects,
> other than overruling a possible object with the name A
> 
> def some_function ( ...) :
>      A = object ( ...)
>      sys._getframe(1).f_globals [ Name ] = A
> 
> 

Anything wrong with the 'global' statement ?

Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
pythonrc start
pythonrc done
 >>> def foo():
...     global a
...     a = 42
...
 >>> a
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
 >>> foo()
 >>> a
42
 >>>





More information about the Python-list mailing list