Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the semantics of the 'is' operator in Python?

Tags:

python

How does the is operator determine if two objects are the same? How does it work? I can't find it documented.

like image 934
bodacydo Avatar asked Mar 13 '10 14:03

bodacydo


People also ask

What is the is operator in Python?

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.

What are semantics in Python?

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.

Is operator syntax in Python?

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.

What is Python syntax and semantics?

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.


1 Answers

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.

like image 161
danben Avatar answered Sep 19 '22 13:09

danben