Python dumps core with this.. how come?

Fredrik Lundh fredrik at pythonware.com
Sat Jan 22 10:45:06 EST 2000


Roey Katz <katz at glue.umd.edu> wrote:
> >> Also, I have another question (very old, and I can't find this in the
> >> FAQ):  I want to explicitly take a reference to an integer.
> >
> >1. reset your brain ;-)
> >2. variables are named references, not small boxes
> >   that hold data values
> >3. *all* variables in python are references.  there
> >   is no other thing.
> >4. integers cannot be modified in place.
> 
> Ok, OK:  this is what I always get when I ask this question,
> but then why does this happen:
> 
>      >>> a = 3 
>      >>> b = a 
>      >>> b
>      3 
>      >>> a = 5
>      >>> b
>      3

you missed points (1) and (2) in my mail ;-)

to make it clearer, here's one more:

5. simple assignments don't copy any data,
   they rebind the name

(here, a simple assignment is an assignment
of the form "name = expression".  more com-
licated assignments like "name[index] = value"
are syntactic sugar; they map to method calls
that modify the object referred to by "name")

so the second assignment associates the
name "a" with *another* integer value.

it does *not* modify the contents of the
small box that hold the data value...

any name (like "b") that refers to the
old value still refers to the old value.

got it?

> although that I cannot modify numbers, strings
> or tuples in-place seems to me inconsistent

why on earth do you need to change the value
of "1"?

(if that question doesn't make sense, reset your
brain and start over from (1) ;-)

(sorry, no time to elaborate further.  gotta run.
see you all at the conference!)

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
— see it live on the python conference! &smiley; -->





More information about the Python-list mailing list