Python Boolean Logic
Steve D'Aprano
steve+python at pearwood.info
Sat Sep 23 01:47:13 EDT 2017
On Sat, 23 Sep 2017 03:01 pm, Bill wrote:
> s='(20 - 10) > 15'
> b=(20 - 10) > 15
> print(s, " is ", ("true" if b else "false") ); ## inside parentheses
> may be removed.
>
> I am new to Python. Maybe someone here is familiar with an elegant way
> to get the the value of b directly from the string s? Hmm... It appears
> that eval() would work
Indeed it will, but don't get into the habit of using eval willy-nilly. While it
is absolutely fine to use it with data you provide yourself, it is a HUGE
security risk to eval strings that came from an untrusted user.
eval("__import__('os').system('echo """rm-rf /"""')")
Also, for what its worth, it's about ten times slower to run:
eval('(20 - 10) > 15')
than to simply run
(20 - 10) > 15
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list