-1

In Python 3...

  • Sample 1:
s: str = None
b: bool = True
b = (s != None) and (s != "some-not-allowed-value")
print (b)

Displays False (as it seems intuitive)


  • Sample 2:
s: str = None
b: bool = True
b = (s) and (s != "some-not-allowed-value")
print (b)

Displays None

Why ? How is that s field evaluated ?

Serban
  • 592
  • 1
  • 4
  • 24

1 Answers1

-1

In python, None interacts as follows with booleans:

>>> print(None and False)
None
>>> print(None and True)
None
>>> print(None or False)
False
>>> print(None or True)
True

Note that the type hints (s: str) are only hints, and are ignored at Runtime

Florent Monin
  • 1,278
  • 1
  • 3
  • 16