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

Discussion in 'Python' started by Stef Mientki, Jul 1, 2009.

  1. Stef Mientki

    Stef Mientki Guest

    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


    thanks,
    Stef Mientki
    Stef Mientki, Jul 1, 2009
    #1
    1. Advertising

  2. Re: Adding an object to the global namespace through " f_globals"is that allowed ?

    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
    >>>
    Bruno Desthuilliers, Jul 3, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    5
    Views:
    10,033
    Steven Cheng[MSFT]
    Aug 3, 2004
  2. Steve Richter
    Replies:
    3
    Views:
    3,375
    Brock Allen
    Apr 5, 2005
  3. Bas
    Replies:
    3
    Views:
    538
  4. Terry Reedy
    Replies:
    3
    Views:
    324
    Chris Rebert
    Jul 3, 2009
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    497
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page