a question from a newcomer to this language
Hi, I am a undergraduate physics student, and I have been asked to write some code in Python for my summer job. Over the past few days, I have been learning Python and Tkinter, so naturally, I have several questions about the language. Here's one: Is there any way to convert a string into an instruction that will be executed? For example, suppose there's a string sString = "z = x*y". Is there any way to have the instruction z = x*y be executed by performing some operation on sString? Thanks in advance for your help. Shankar -- http://mail.python.org/mailman/listinfo/python-list
RE: a question from a newcomer to this language
Shankar writes: > Is there any way to convert a string into an instruction that will be > executed? Short answer: Yes. The exec statement does what you want: >>> x = 3 >>> y = 4 >>> exec "z = x * y" >>> print z 12 HOWEVER... the long answer is that you almost certainly do NOT want to use exec. Nearly everything that can be done with exec can be done without it and the solution that does NOT use exec is faster, more understandable, and has better security features. Often the solution that does not use exec will be simpleer and more elegant as well. If you look at a problem and are nearly certain that it needs to be solved using exec, try posting it here... the people on this newsgroup are very good at solving challenges like that. But try it yourself first... you may learn something. -- Michael Chermside -- http://mail.python.org/mailman/listinfo/python-list
Re: a question from a newcomer to this language
Michael Chermside wrote: > Shankar writes: > >>Is there any way to convert a string into an instruction that will be >>executed? > > > Short answer: > Yes. The exec statement does what you want: > > x = 3 y = 4 exec "z = x * y" print z > > 12 > Ooh! I didn't know that one. I have to admit that it gives me an uneasy feeling. How woud the author of "z = x * y" know that z was safe to assign to? To Shankar: An intermediate is eval: z = eval("x * y") but even this is unsafe if you do not vet the input strings: String s = "system('format c: /y')" z = eval(s) Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: RE: a question from a newcomer to this language
Thank you for your help. I will post the problem in more detail if I find that I cannot avoid using exec. Shankar - Original Message - From: Michael Chermside <[EMAIL PROTECTED]> Date: Friday, June 10, 2005 3:49 pm Subject: RE: a question from a newcomer to this language > Shankar writes: > > Is there any way to convert a string into an instruction that > will be > > executed? > > Short answer: >Yes. The exec statement does what you want: > > >>> x = 3 > >>> y = 4 > >>> exec "z = x * y" > >>> print z > 12 > > HOWEVER... the long answer is that you almost certainly do NOT want > to use exec. Nearly everything that can be done with exec can be > done without it and the solution that does NOT use exec is faster, > more understandable, and has better security features. Often the > solution that does not use exec will be simpleer and more elegant > as well. > > If you look at a problem and are nearly certain that it needs to be > solved using exec, try posting it here... the people on this newsgroup > are very good at solving challenges like that. But try it yourself > first... you may learn something. > > -- Michael Chermside > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list