Click here to Skip to main content
14,330,528 members
Rate this:
Please Sign up or sign in to vote.
See more:
I'm kind of new to python and how eval works, so any help is appreciated.

I was given an expression of variables that have the following values to check certain strings.

Here is the expression:

eval(("PRES" == "PRES" + "PRES"=="PRES")==1)



Now, perhaps I am misunderstanding how the expression is working, buut from the syntax it seems like an AND to see if both evaluate to true,
but i get a false in return.

Is there some special case for the + or the == 1?

Can anyone clarify what the expression is saying/doing?
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

That code is not even valid, because eval only accepts a string; so if you would want to make it work, you should put quotes around your expression. Aside from that, if your expression is a hard-coded one like this, you don't need eval.

About your expression, actually it evaluates to this:
("PRES" == ("PRES" + "PRES") == "PRES") == 1

("PRES" == "PRESPRES" == "PRES") == 1

False == 1

False

That's what it does.

How to fix it? Use use an and instead of a +. But don't use +, because even if you would use parentheses to make sure it would be ("PRES" == "PRES") + ("PRES" == "PRES") then it would be 2.

So, to make it true, your expression should look like this:
"PRES" == "PRES" and "PRES" == "PRES"

==1 is not necessary; because it would still be True if the above expression is also True.
   
Comments
Ramza360 31-Dec-14 12:55pm
   
Ok this is just an example. However the issue is how the variables are given to me to evaluate in the above layout (e.g. (string == string + string == string) == 1). Was thinking maybe the plus meant something else in python but I guess the only way around would be to do a replace on the + as and, but that would break any expression containing the + in it.. Weird situation.

Thank you for the above explanation though, just wondering why they would think that would evaluate correctly then..
Thomas Daniels 31-Dec-14 12:59pm
   
I don't know who thinks it would evaluate correctly, and I also don't know why they would think that. The + doesn't mean anything really special in this case, it just concatenates the two strings.
Ramza360 31-Dec-14 13:03pm
   
Very interesting, and that's kind of what i expected to. Only other thing i was thinking was if they thought the cases were doing the boolean check and replaceing the bool with its integer (true:1) (false:0) and seeing if they add up to 1. Ohwell.

Thanks for the quick reply.
Sergey Alexandrovich Kryukov 31-Dec-14 13:55pm
   
My 5. However, everything was correct only before you mentioned "how to fix it". It cannot and should not be fixed, this is the case when the main thing should be fixed: someone's brain, and you did the best by your answer and comments.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100