Looking forward to evaluating boolean expressions in Python

Is there a way (using evalor something else) to evaluate intolerably boolean expressions in python?

We will see:

>>> x = 3
>>> 5 < x < y
False

Hop! This is very nice because it will be false, regardless of value y. The fact is that it ycan even be undefined, and I would like to get this exception. How can I get python to evaluate all expressions even if it knows the result in advance?

I hope I get it! Thanks
Manuel

Edit: Please keep in mind that the expression should not be changed, just an evaluation method.

+3
source share
6
all([5 < x, x < y])
+5
(5 < x) & (x < y)

- &, ( and, or, chaining, all/any). ( ), , ; -).

+6

, , .

a = foo()
b = bar()
if a and b:
    ...

, all([5 < x, x < y]), , , (&) - , , , , , ???. , , .

NameError, b (.. ), a - false, , , bugfinder, .

+5
>>> x = 3
>>> y > x > 5
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'y' is not defined
+3

, , , . , x or y() x() or y() . , , pylint, pyflakes pychecker.

+2

If you receive instructions from the user and want to execute it using your own semantics, you must analyze it yourself with a tool such as pyparsing. It is dirty and insecure to evaluate the code of another in the middle of yours, mixing their results with yours, and this is confused to evaluate what looks like Python code, but with different semantics.

+1
source

Source: https://habr.com/ru/post/1732965/


All Articles