Reference

Rustom Mody rustompmody at gmail.com
Mon Mar 3 16:52:29 CET 2014


On Monday, March 3, 2014 3:12:30 PM UTC+5:30, ast wrote:
> hello

> Consider following code:

> >>> A=7
> >>> B=7
> >>> A is B
> True

> I understand that there is a single object 7 somewhere in memory and
> both variables A and B point toward this object 7

> now do the same with a list:

> >>> l1 = [1, 2]
> >>> l2 = [1, 2]
> >>> l1 is l2
> False

> It seems this time that there are 2 distincts objects [1, 2] in memory. l1 points
> toward the first one and l2 points toward the second one.

> if I change one, the second remains unchanged

> >>> l1.append(3)
> >>> l1
> [1, 2, 3]
> >>> l2
> [1, 2]

> I dont really understand why the behavior is different. 
> Both integer 7 and list [1, 2] are objects. Why is it
> different ?

Short answer: Avoid using 'is'. 

Long answer: http://www.beyondwilber.ca/healing-thinking/non-identity-korzybski.html

Pragmatic answer: Think of 'is' as a short-form for 'machine-rep-is'
And use machine representations with the same alacrity that a
C programmer uses inline assembly



More information about the Python-list mailing list