Functional equivalence of Statement Python logic

I am trying to find the functional equivalent of logical operators in Python (e.g. and / or / not). I thought I found them in a module operator, but the behavior is different from the others.

For example, the operator andexecutes the behavior that I want, while it operator.and_seems to require explicit type comparison, otherwise it throws a TypeError exception.

>>> from operator import *
>>> class A: pass
... 
>>> and_(A(), True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'instance' and 'bool'
>>> A() and True
True

Is there something like a module operatorbut contains functions that exactly match the behavior of the logic of the Python operator?

+3
source share
1 answer

operator.and_ operator.or_ - "" "" . operator , and or, any() all(). , , .

, and or .

False and sys.stdout.write("Hello")

,

f(False, sys.stdout.write("Hello"))

, , f() . , operator.

+9

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


All Articles