unexpected behavior: did i create a pointer?

Arnaud Delobelle arnodel at googlemail.com
Sun Sep 9 05:30:00 EDT 2007


On Sep 8, 10:44 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
[...]
> Ways that Python objects are like pointers:
>
> (1) ... um...
>
> Oh yeah, if you bind the _same_ object to two different names, _and_ the
> object is mutable (but not if it is immutable), mutating the object via
> one name will have the same effect on the object -- the same object,
> naturally -- bound to the other name.

Well one of the main uses of pointers in C is as things that *point
to* objects.  And AFAIK that's exactly what a name is in Python.  In
fact I think to say that  a name points to (or refers to) an object is
less misleading that to say it is bound to.  Binding implies some sort
of symmetry but when I write:

a = "Am I bound?"

The name a knows it's refering to the string object, whereas the
string has no idea who refers to it (well an implementation might want
to store this information, but it is inaccessible).

> You know, maybe because I came to Python with no C experience, I never
> had trouble with the "unexpected behaviour" that so confused the original
> poster. It's just obvious.

The funny thing is that if the OP had thought of both 'a' and
'copyOfA' as C-like pointers then he wouldn't have been confused :)

--
Arnaud





More information about the Python-list mailing list