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

Chris Rebert clp2 at rebertia.com
Fri Jul 3 04:15:35 EDT 2009


On Fri, Jul 3, 2009 at 1:07 AM, Stef Mientki<stef.mientki at gmail.com> wrote:
> ryles wrote:
>
> On Jul 2, 1:25 am, Terry Reedy <tjre... at udel.edu> 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



More information about the Python-list mailing list