anything like C++ references?

Erik Max Francis max at alcyone.com
Mon Jul 14 03:07:44 EDT 2003


Stephen Horne wrote:

> Imagine, for instance, changing all assignments and other 'copying' to
> use a copy-on-write system. Then add a pointer type and a 'newcopyof'
> operator. And nick the C-style prefix '*' for dereferencing and add
> '&' as a 'make a pointer to this object' operator (absolutely NOT a
> physical memory address).

The problem is that this misses the point that Python is not C++.  You
shouldn't try grafting syntaxes and approaches that make Python look
more like C++, you should be learning to use Python on its own merits.

If what you want is a "pointer" -- something which encapsulates a
reference to some other object which can be changed -- then you can do
this very easily in Python through containment (either the
single-element list, or a dedicated, and thus probably more clear,
Container class like I gave at the outset of this thread).  You're
saying you want something which misses the point of Python's handling of
variables:  Variables are not slots where objects are placed, their
Post-It notes you can paste onto objects.  Post-It notes can be added
and removed at will, and some names can be different in different
scopes.

Everything is handled the same way; it's just that some objects are
mutable in Python and some are not.  You can say that immutability is
_equivalent_ to having those types of objects treated separately, but
that's not the same thing as them _actually_ being treated differently,
because as it's been made clear here they are not.  You're drawing an
arbitrary line in the sand, but the line you're drawing is in the wrong
place (assignment doesn't work differently on mutable and immutable
objects -- with the exception of the extended += operators mentioned
earlier) and is not particularly arbitrary.  Already examples of C code
that will choke based on their implicit immutability, mentioned nowhere
in the code by use of const, has been given; that makes C++'s mutability
issues far more arbitrary than Python's!

Or, to put it another way, if you want to program in C++, why not use
C++?

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ A life without festivity is a long road without an inn.
\__/  Democritus




More information about the Python-list mailing list