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

Discussion in 'Python' started by Terry Reedy, Jul 2, 2009.

  1. Terry Reedy

    Terry Reedy Guest

    Stef Mientki wrote:
    > 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


    global name
    name = A

    or is name is a string var
    globals()[name] = A
     
    Terry Reedy, Jul 2, 2009
    #1
    1. Advertising

  2. Terry Reedy

    ryles Guest

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

    On Jul 2, 1:25 am, Terry Reedy <> wrote:
    > > 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

    >
    > global name
    > name = A
    >
    > or is name is a string var
    > globals()[name] = A


    It wasn't explicit, but I think Stef meant that the global should be
    added to the caller's environment, which was the reason for
    sys._getframe().

    Is this environment only intended for interactive use? I wonder if you
    might just set things up
    in a PYTHONSTARTUP script instead.
     
    ryles, Jul 3, 2009
    #2
    1. Advertising

  3. Terry Reedy

    Stef Mientki Guest

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

    ryles wrote:
    > On Jul 2, 1:25 am, Terry Reedy <> wrote:
    >
    >>> 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
    >>>

    >> global name
    >> name = A
    >>
    >> or is name is a string var
    >> globals()[name] = A
    >>

    >
    > It wasn't explicit, but I think Stef meant that the global should be
    > added to the caller's environment, which was the reason for
    > sys._getframe().
    >
    > Is this environment only intended for interactive use? I wonder if you
    > might just set things up
    > in a PYTHONSTARTUP script instead.
    >

    the idea is to get a simple environment where you can do interactive 3D
    geometry,
    without knowing anything about Python.
    So to create a triangle e.g., the whole program will look like this:

    Point ( 'A', (1,1,0) )
    Point ( 'B', (5,5,0) )
    Point ( 'C', (1,5,0) )
    Line_Segment ( 'AB' )
    Line_Segment ( 'BC' )
    Line_Segment ( 'AC' )



    And now the points A,B,C and the line segments AB,BC,AC also exists as
    variables in the namespace of the above environment.
    So now you can add a point "D" in the middle of the line-segment AB, by
    giving the formula of that point:

    Point ( 'D', ' ( A + B ) / 2 ' )

    Or display the properties of point A, with :

    Print A

    which (for the moment) will result in:

    Point Instance: A
    Childs : AB(Line-Segment), AC(Line-Segment),
    Parents:

    The graphics above is done in VPython, which gives you an easy way to
    make all objects dragable,
    and if objects are defined by formulas, the will stick together
    according to that formula.

    And now it's obvious I can't use the solution of Terry,
    because I don't know what names will to become global.

    thanks,
    Stef
     
    Stef Mientki, Jul 3, 2009
    #3
  4. Terry Reedy

    Chris Rebert Guest

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

    On Fri, Jul 3, 2009 at 1:07 AM, Stef Mientki<> wrote:
    > ryles wrote:
    >
    > On Jul 2, 1:25 am, Terry Reedy <> wrote:
    >
    >
    > 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
    >
    >
    > global name
    > name = A
    >
    > or is name is a string var
    > globals()[name] = A
    >
    >
    > It wasn't explicit, but I think Stef meant that the global should be
    > added to the caller's environment, which was the reason for
    > sys._getframe().
    >
    > Is this environment only intended for interactive use? I wonder if you
    > might just set things up
    > in a PYTHONSTARTUP script instead.
    >
    >
    > the idea is to get a simple environment where you can do interactive 3D
    > geometry,
    > without knowing anything about Python.
    > So to create a triangle e.g., the whole program will look like this:
    >
    > Point ( 'A', (1,1,0) )
    > Point ( 'B', (5,5,0) )
    > Point ( 'C', (1,5,0) )
    > Line_Segment ( 'AB' )
    > Line_Segment ( 'BC' )
    > Line_Segment ( 'AC' )
    >
    >
    >
    > And now the points A,B,C and the line segments AB,BC,AC also exists as
    > variables in the namespace of the above environment.
    > So now you can add a point "D" in the middle of the line-segment AB, by
    > giving the formula of that point:
    >
    > Point ( 'D',  ' ( A + B ) / 2 ' )
    >
    > Or display the properties of point A, with :
    >
    > Print A
    >
    > which (for the moment) will result in:
    >
    > Point Instance: A
    >   Childs : AB(Line-Segment), AC(Line-Segment),
    >   Parents:
    >
    > The graphics above is done in VPython, which gives you an easy way to make
    > all objects dragable,
    > and if objects are defined by formulas, the will stick together according to
    > that formula.
    >
    > And now it's obvious I can't use the solution of Terry,
    > because I don't know what names will to become global.


    Have you considered just using imports instead? Requiring a single opaque:

    from graphicslibthingy import *

    at the start of a file using the environment isn't so bad, IMHO.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Jul 3, 2009
    #4
    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,043
    Steven Cheng[MSFT]
    Aug 3, 2004
  2. Steve Richter
    Replies:
    3
    Views:
    3,381
    Brock Allen
    Apr 5, 2005
  3. Bas
    Replies:
    3
    Views:
    543
  4. Stef Mientki
    Replies:
    1
    Views:
    314
    Bruno Desthuilliers
    Jul 3, 2009
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    506
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page