0

I have a function that returns a string that can be interpreted as a Boolean

def foo():
    return "not(True and False)"

How can I return the actual Boolean value, in this case True?

5
  • Yes. I want not(True) to be False, (True or False) to be True. Whatever the results would be If the quotes would not be there. Commented Feb 11, 2020 at 4:20
  • I think eval() will probably already work, have you tried using this function? Commented Feb 11, 2020 at 4:21
  • 1
    Why does your function have such a strange return value? The best approach is likely to change the function. Commented Feb 11, 2020 at 4:22
  • Python 3, I want to return the actual Boolean value. Commented Feb 11, 2020 at 4:22
  • 1
    I have a function that returns a string that can be interpreted as a Boolean What, why? Commented Feb 11, 2020 at 4:30

1 Answer 1

0

You could use eval:

def foo():
    return eval("not(True and False)")

Even tho it is virtually the only solution, but it is still not safe.

Check this link for why it is not safe.

Sign up to request clarification or add additional context in comments.

Comments