[Tutor] An identity question

Cecilia Alm ebbaalm at uiuc.edu
Sun Apr 22 21:24:56 CEST 2007


OK. I guess the same holds for the below list-internal values. It
doesn't seem that this could lead to unexpected aliasing 'conflict'
when assigning new values to names.
Thanks,
C

>>> def foo3():
...    x = [232131, 321321, 432432]
...    return x
...
>>> a = foo3()
>>> b = foo3()
>>> a is b
False
>>> a[0] is b[0]
True
>>> a[0] = 100
>>> a[0] is b[0]
False
>>> a
[100, 321321, 432432]
>>> b
[232131, 321321, 432432]



2007/4/22, Kent Johnson <kent37 at tds.net>:
> Cecilia Alm wrote:
> > 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 4:
> >>>> def foo():
> > ...     x = 10000
> > ...     return x
> > ...
> >>>> A = foo()
> >>>> B = foo()
> >>>> A is B
> > True
>
> I think in this case foo() has an internal reference to the constant
> 10000. It returns the same reference each time.
>
> Kent
>


-- 
E. Cecilia Alm
Graduate student, Dept. of Linguistics, UIUC
Office: 2013 Beckman Institute


More information about the Tutor mailing list