Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Using string as condition in if statement

I am trying to do this:

a = "1 or 0"
if (a): Print "true"

So I can use a string as the condition of an if statement. Is this possible?

like image 859
user2351418 Avatar asked May 05 '13 07:05

user2351418


1 Answers

It's possible, but don't do it:

a = '1 or 0'

if eval(a):
    print 'a is True'

eval() is hard to debug, offers no benefit here and is easily exploitable if you allow arbitrary user input (e.g. a = "__import__('os').system('rm -Rf /')").

like image 188
Blender Avatar answered Oct 26 '22 07:10

Blender