[Tutor] An identity question

Andreas Kostyrka andreas at kostyrka.org
Sun Apr 22 22:41:32 CEST 2007


* Cecilia Alm <ebbaalm at uiuc.edu> [070422 22:34]:
> The differences in cases 1 and 3 vs. 2 is due to 'common values' of
> name assignments being treated a bit differently, right? Also, it's
> clear why case 5 evaluates to false.  But, why does the case 4
> equality check evaluate to True, whereas case 1 and 3 don't?

Case 1 returns False, because a and b are different objects (albeit
representing the same value)
Case 2 returns True, because small integers return always the same
object (it's an optimization).
Case 3 returns False, because the integer objects are generated again
seperatly.
Case 4 returns True, because x is always the same object, created
inside of foo()
Case 5, well 10000 != 10001.

Andreas

> 
> case 1:
> >>> a = 10000
> >>> b = 10000
> >>> a is b
> False
> 
> case 2:
> >>> a = 10
> >>> b = 10
> >>> a is b
> True
> 
> case 3:
> >>> def foo2(v):
> ...     return v
> ...
> >>> A1 = foo2(10000)
> >>> B1 = foo2(10000)
> >>> A1 is B1
> False
> 
> case 4:
> >>> def foo():
> ...     x = 10000
> ...     return x
> ...
> >>> A = foo()
> >>> B = foo()
> >>> A is B
> True
> 
> case 5:
> >>> C1 = foo2(10000)
> >>> D1 = foo2(10001)
> >>> C1 is D1
> False
> 
> Thanks!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list