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?
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 /')"
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With