newbie looking for help, int reference question

Steve Holden sholden at holdenweb.com
Tue Jan 14 12:36:35 EST 2003


"jodocus" <johocus at zonnet.nl> wrote ...
> hi,
>
> i am learning python, but at the moment i cannot afford to buy a book on
> the subject. So i am trying to find all the appropriate information on the
> web. This proves difficult, so if anyone knows a good (complete) online
> book/resource about the language, i would like to know. I am looking for
> more than just a tutorial (these are not hard to find), but rather a
> complete work that describes the details of the language.
>
> A thing I just ran into and couldn't find in the tutorials i am using:
>
> >>> i = 10
> >>> j = i
> >>> i is j
> 1
> >>> i += 10
> >>> i
> 20
> >>> j
> 10
>
> it seems that, after adding a value, i has become another object. So i
> guess the + operator makes a new object, does this mean that ints are
> 'immutable'?

That's absolutely what it means. Congratulations!

> How do i make sure that j changes when i does? Is this
> comparable to the Java distinction between objects and primitive types,
> where objects are referenced but primitives are not?
>
I'm afraid there isn't any way to do that, precisely because of the
immmutability of integers.

> As a C/C++ programmer i really like to know whether i am dealing with
> pointers or not, this makes me feel more sure about the correctness of the
> code i am writing.
>
Well, all Python names are references, if that helps. Note, though, that
object attributes and compund object elements are also references. How about
this:

>>> l = [10, 20, 30]
>>> k = l[0]
>>> j = l[0]
>>> l[0] += 1
>>> k is j
1
>>> j
10
>>> k
10
>>> l
[11, 20, 30]
>>>

> Thanks a lot,
>
A pleasure. I hope it helped!

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Bring your musical instrument to PyCon!    http://www.python.org/pycon/







More information about the Python-list mailing list