Thursday, November 05, 2009

[Py] Equal sign

Python: Careful with equal sign

I found this article when I was searching for the information about comparisons in Python.

When we assign a variable to another, for example, a = 1; b = a, Python just passes the reference (of variable a) to the new one (i.e., the variable b) but not creates a new copy with new memory address.

>>> a = 1
>>> b = a
>>> id(a) == id(b)
>>> True

But if we change the value of b, then a new memory block will be used to store the new assigned value.

>>> b = 2
>>> id(a) == id(b)
>>> False

No comments:

Post a Comment