Adding an object to the global namespace through " f_globals" is that allowed ?
Stef Mientki
stef.mientki at gmail.com
Fri Jul 3 10:07:50 CEST 2009
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.
thanks,
Stef
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090703/f1a53503/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: moz-screenshot-3.jpg
Type: image/jpeg
Size: 3333 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090703/f1a53503/attachment.jpg>
More information about the Python-list
mailing list