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?

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.