anything like C++ references?

Donn Cave donn at drizzle.com
Sun Jul 13 23:40:05 EDT 2003


On Sun, 2003-07-13 at 20:32, Aahz wrote:
> In article <mailman.1058126720.6756.python-list at python.org>,
|> Ian Bicking  <ianb at colorstudy.com> wrote:
|>>
|>> (Admittedly, some confusion may occur because these very different
|>> operations use the same syntax:
|>>
|>>  x = 10
|>>  x[0] = 10
|>>  obj.x = 10
|>>
|>> The second and third are entirely different from the first.)
|> 
|> No, they aren't.  They are precisely the same; they just have different
|> assignment targets.
|
| Sure they are different.  The first is a primitive operation binding the
| variable x.  The second gets x, and calls x.__setitem__(0, 10), and the
| third is equivalent to setattr(obj, 'x', 10).

I wonder if this is a good example of a mutability thing that really
is kind of missing to the detriment of Python.

If you want to support user-implemented sequences (as Python does)
but make Aahz's ``precisely the same'' assertion true in the sense
you're taking it - then I think the user-implemented indexing function
would have to return a reference to the index location.

   class UserArray:
       ...
       def __item__(self, index):
           return &self.internal_array[index]

   userArray[5] = 1
   (python internal)
       loc = UserArray.__item__(userArray, 5)
       *loc = 1

   if userArray[6] == 
   (python internal)
       loc = UserArray.__item__(userArray, 6)
       return *loc

There are other places where instead we use some kludge with a
mutable container type, but in this case I don't think there's
a way to get around it.  So whether we like the idea or not,
assignment to a user-implemented sequence doesn't have to involve
any assignment at all, because Python lacks the "pointer" type
(or "target" type, if you prefer) that would be required to
implement that.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list