Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


stef.mientki at gmail

Jul 1, 2009, 1:08 PM

Post #1 of 7 (410 views)
Permalink
Adding an object to the global namespace through " f_globals" is that allowed ?

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
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Jul 1, 2009, 10:25 PM

Post #2 of 7 (378 views)
Permalink
Re: Adding an object to the global namespace through " f_globals" is that allowed ? [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


rylesny at gmail

Jul 2, 2009, 7:55 PM

Post #3 of 7 (373 views)
Permalink
Re: Adding an object to the global namespace through " f_globals" is that allowed ? [In reply to]

On Jul 2, 1:25 am, Terry Reedy <tjre...@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.
--
http://mail.python.org/mailman/listinfo/python-list


stef.mientki at gmail

Jul 3, 2009, 1:07 AM

Post #4 of 7 (368 views)
Permalink
Re: Adding an object to the global namespace through " f_globals" is that allowed ? [In reply to]

ryles wrote:
> On Jul 2, 1:25 am, Terry Reedy <tjre...@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
Attachments: moz-screenshot-3.jpg (3.25 KB)


clp2 at rebertia

Jul 3, 2009, 1:15 AM

Post #5 of 7 (365 views)
Permalink
Re: Adding an object to the global namespace through " f_globals" is that allowed ? [In reply to]

On Fri, Jul 3, 2009 at 1:07 AM, Stef Mientki<stef.mientki [at] gmail> wrote:
> ryles wrote:
>
> On Jul 2, 1:25 am, Terry Reedy <tjre...@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
--
http://mail.python.org/mailman/listinfo/python-list


bruno.42.desthuilliers at websiteburo

Jul 3, 2009, 5:50 AM

Post #6 of 7 (367 views)
Permalink
Re: Adding an object to the global namespace through " f_globals" is that allowed ? [In reply to]

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
>>>


--
http://mail.python.org/mailman/listinfo/python-list


stef.mientki at gmail

Jul 5, 2009, 2:35 AM

Post #7 of 7 (343 views)
Permalink
Re: Adding an object to the global namespace through " f_globals" is that allowed ? [In reply to]

Terry Reedy wrote:
> 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
great, the last 2 lines works in most cases.
Now to get everything working correctly, I have to use both globals()
and f_globals()
but everything seems to work perfect now.

thanks for all the hints.
cheers,
Stef

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.