On 2014-03-03, ast <nom...@invalid.com> 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
Maybe, maybe not. Integer are immutable, so that's allowed but not required. In CPython, that's true for small integers, but that is an implementation detail, and you shouldn't depend on it. > and both variables A and B point toward this object 7 They might. They might not. > 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. Yep. Lists are mutable, therefore each literal produces a distinct object. > l1 points toward the first one and l2 points toward the > second one. Yep. > I dont really understand why the behavior is different. > Both integer 7 and list [1, 2] are objects. Why is it > different ? Integer objects are immutable (they can't change value), therefore you can reuse them without causing problems. Lists are mutable (you can change the values in them), so you can't reuse them. -- Grant Edwards grant.b.edwards Yow! Maybe I should have at asked for my Neutron Bomb gmail.com in PAISLEY -- -- https://mail.python.org/mailman/listinfo/python-list