[Tutor] Follow-up Qs: Re: Identity operator (basic types)

Cecilia Alm ebbaalm at uiuc.edu
Sun Feb 11 14:59:54 CET 2007


Thanks for the respones. A few follow-up questions:

For these basic types (float, integer, string, char, bool) does python
always figure out the identity change when assigning a 'new value',
as it seems to do below?

>>> i = "hi"
>>> j = i
>>> print i, j, id(i), id(j)
hi hi 12235136 12235136
>>> j += i
>>> print i, j, id(i), id(j)
hi hihi 12235136 13579872

In other words, could there ever be a risk of overwriting the original value
when copying variables, like i=j?

Does one ever have to worry about using copy.deepcopy(i) for these basic
types (float, integer, string, char, bool) to avoid overwriting the
value of the original (like one may have to do for lists and dictionaries?)

When we copy any such data type (float, integer, string, char, bool) into a
function definition's local scope, does it always copy-by-value then? (so
one does not have to worry about the original variable's identity and value
being changed?)

>>> def sum(k):
    print k, id(k)
    k += 1
    print k, id(k)


>>> a = 1234
>>> print a, id(a)
1234 12536936
>>> sum(a)
1234 12536936
1235 12536876
>>> print a, id(a)
1234 12536936
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070211/8d0968c2/attachment.html 


More information about the Tutor mailing list