Re: Using eval with substitutions

2006-09-02 Thread Anthra Norell
- Original Message - From: "Carl Banks" <[EMAIL PROTECTED]> Newsgroups: comp.lang.python To: Sent: Saturday, September 02, 2006 6:33 AM Subject: Re: Using eval with substitutions > [EMAIL PROTECTED] wrote: > > >>> a,b=3,4 > > >>> x=&quo

Re: Using eval with substitutions

2006-09-01 Thread Carl Banks
[EMAIL PROTECTED] wrote: > >>> a,b=3,4 > >>> x="a+b" > >>> eval(x) > 7 > >>> y="x+a" > > Now I want to evaluate y by substituting for the evaluated value of x. > eval(y) will try to add "a+b" to 3 and return an error. I could do > this, > >>> eval(y.replace("x",str(eval(x > 10 > > but this beco

Re: Using eval with substitutions

2006-09-01 Thread abhishek
Thanks very much for all your suggestions - Peter, Duncan and Frederic. It was a great help. Incidentally the question arose while I was trying to solve the 2-nots problem, http://www.inwap.com/pdp10/hbaker/hakmem/boolean.html#item19 Part of my program takes a set of boolean expressions in 3 vari

Re: Using eval with substitutions

2006-08-31 Thread Anthra Norell
Abhishek, I hesitate to propose this idea, because there's got to be a better (more conventional) way of doing this. Anyway consider this: >>> x = "a+b"; y = "x*a; z = "x+y" # Your definitions >>> import SE >>> def f (x, y, z): substitutions = 'z=(%s) | y=(%s) | x=(%s)' % (z,

Re: Using eval with substitutions

2006-08-31 Thread Duncan Booth
[EMAIL PROTECTED] wrote: > So as you say, I could do: > x=eval(x), y=eval(y), z=eval(z) and finally eval("z+y+x") but the > problem is that the initial strings are in no particular order, so I > don't know the sequence in which to perform the first 3 evaluations. I > was wondering if there was a s

Re: Using eval with substitutions

2006-08-31 Thread Peter Otten
[EMAIL PROTECTED] wrote: > Fredrik Lundh wrote: >> (I'm afraid I don't really understand the point of your examples; what >> is it you're really trying to do here ?) > > A function is passed a bunch of string expressions like, > x = "a+b" > y= "x*a" > z= "x+y" > > where a and b are assumed to h

Re: Using eval with substitutions

2006-08-31 Thread abhishek
Fredrik Lundh wrote: > (I'm afraid I don't really understand the point of your examples; what > is it you're really trying to do here ?) A function is passed a bunch of string expressions like, x = "a+b" y= "x*a" z= "x+y" where a and b are assumed to have been assigned values in the local namesp

Re: Using eval with substitutions

2006-08-31 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: a,b=3,4 x="a+b" eval(x) > 7 y="x+a" > > Now I want to evaluate y by substituting for the evaluated value of x. > eval(y) will try to add "a+b" to 3 and return an error. I could do > this, eval(y.replace("x",str(eval(x > 10 > > but this become