How does the is
operator determine if two objects are the same? How does it work? I can't find it documented.
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.
Python uses dynamic semantics, meaning that its variables are dynamic objects. Essentially, it's just another aspect of Python being a high-level language. In the list example above, a low-level language like C requires you to statically define the type of a variable.
The operator can be defined as a symbol which is responsible for a particular operation between two operands. Operators are the pillars of a program on which the logic is built in a specific programming language.
The syntax of a programming language refers to structure of the language, that is, what constitutes a legal program. The semantics of a programming language refers to the meaning of a legal program.
From the documentation:
Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address).
This would seem to indicate that it compares the memory addresses of the arguments, though the fact that it says "you may think of it as the object's address in memory" might indicate that the particular implementation is not guranteed; only the semantics are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With