[python-win32] How Can I exec() a statement?

Gabriel Genellina gagenellina at softlab.com.ar
Tue Apr 12 04:59:35 CEST 2005

At Thursday 7/4/2005 10:53, * William wrote:

Please stay in the list - and even move to the Python-list as those topics 
are not specific to Windows.

>Exactly what I want to do -- for the enquiry about "what" I want to do, it 
>is best to use as an example.
>b = 2
>print "Before:"
>print "   a = %s" % str( a )
>print "   b = %s" % str( b )#
>codeLine = 'a = b + 6'
>exec codeLine  in globals(), locals()
>print "After:"
>print "   a = %s"% str( a )
>print "   b = %s" % str( b )
>... of course the notion, is a little more elaborate though still in its 
>infancy.  I am thinking it will be a basic verification tool or even an 
>aid for debugging.  By chopping up the line, I can print the value of all 
>the symbols in the codeLine.  The 'trick' is going to be Not calling 
>functions twice (in case there is a side-effect).

So you are going to write your own debugger? I'd try the existing ones first :)

It's hard to recognize automatically the relevant symbols, even using the 
tokenize/token/parser modules.
Unles you restrict yourself to very simple expressions with no objects, no 
attribute selection, no subscripting...
There is a lot of names there: a,round,math,sin,pi,obj... Some just make 
sense in the context of another (by example, there is no phase variable, 
it's an attribute of obj). Maybe current_angle is a property and getting 
its value actually means invoking a function wich reads a sensor...

>  For example, I'd like a way to get the symbol name for variable a 
> above.  Is there a function like "nameOF( a )"?

Maybe a Python hacker guru could say something, but AFAIK, once you get 
inside the function nameOF( a ), there is no way to tell where its argument 
came from. In the following example, I think there is no way to distinguish 
inside f if it was called with a or b as an argument, since both are simply 
names pointing to the same object:

 >>> def f(x):
...   print x, id(x)
 >>> a = [1,2,3]
 >>> b = a
 >>> a
[1, 2, 3]
 >>> b
[1, 2, 3]
 >>> id(a)
 >>> id(b)
 >>> f(a)
[1, 2, 3] 6999440
 >>> f(b)
[1, 2, 3] 6999440

Gabriel Genellina
Softlab SRL 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20050411/37720264/attachment.html

More information about the Python-win32 mailing list