3

Is there any consensus between languages how tests for emptiness are distinct from tests for noneness? In python the following expression is false:

{} is {}

However this expression evaluates to True

False is False

Why is the first expression not evaluated to True?

Joseph Quinsey
  • 143
  • 2
  • 17
Niklas Rosencrantz
  • 8,008
  • 17
  • 56
  • 95

1 Answers1

7

I think the part you missed is what "is" keyword actually does.

"is" tests references, or "identity" of an object. {} is an empty dictionary, expression "{} is {}" creates two empty dictionary. Their values are "equal" but their identities are not because those two dictionaries reside in different locations in memory as dictionary is an object which is created dynamically.

"False is False" works because it is a constant, so both of those values refer to the same block of memory. Since it makes no sense for Python to allocate different memory blocks every time it sees "False" in the code.

DXM
  • 19,932
  • 4
  • 55
  • 85