For some types in Python, the is
operator seems to be equivalent to the ==
operator. For example:
>>> 1 is 1
True
>>> "a spoon" is "a spoon"
True
>>> (1 == 1) is (2 == 2)
True
However, this is not always the case:
>>> [] == []
True
>>> [] is []
False
This makes sense for immutable types such as lists. However, immutable types such as tuples seem to display the same behavior:
>>> (1, 2) == (1, 2)
True
>>> (1, 2) is (1, 2)
False
This raises several questions:
- Is the
==
/is
equivalence related to immutability? - Are the behaviors above specified, or an implementation detail?
- Most importantly (and basically), how can I know if an assignment will result in a copy of an object being made, or a reference to it being made?
Update:
If assignment is always by reference, why doesn't the following print 2
?:
>>> a = 1
>>> b = a
>>> a = 2
>>> b
1
Why isn't this equivalent to the following C snippet:
int a = 1;
int *b = &a;
a = 2;
printf("%d\n", *b);
Apologies for the newbness of this question, but I am a Python novice and feel that it is important to understand this. Is there any reading you would recommend to understand these sort of issues?