anything like C++ references?

Mark 'Kamikaze' Hughes kamikaze at kuoi.asui.uidaho.edu
Mon Jul 14 19:31:56 EDT 2003


Mon, 14 Jul 2003 04:51:10 +0100, Stephen Horne <intentionally at blank.co.uk>:
> On Sun, 13 Jul 2003 22:42:21 GMT, "Bryan" <belred1 at yahoo.com> wrote:
>>i just referenced an immutable object via a "pointer" and i __did_not__
>>stuff it into a mutable object as you say.
>>a and b "point" to the same object.
> Technically, but what is the point? You can't do pointer-style things
> with it. You can't change the object in any way without changing the
> id, and you can't use the mechanism to, for instance, allow 'var'
> parameters.
> In short, you are showing evidence of the use of pointers internally
> within Python, but that is not the same as providing pointer-like
> semantics.

  It looks like you've almost reached understanding, though you've been
given a bad start with a very idiosyncratic and monolingual
understanding of what "computer science" teaches--that was certainly not
in the classes I took, but apparently yours were different.

  Python variables are directly equivalent to pointers in C, with some
crippling limitations by C standards.  They only ever point to valid
objects that are dynamically allocated on the heap, never to anything
else.  Even None is an object.  Those objects include what Python calls
"integers", which are really object wrappers around a C int variable.
Assignment only allows you to change a variable to point to some other
valid object.

  At no point in Python do you ever have direct access to a C value
variable, or to a reference to a variable.  They just don't exist.  So
there are some things you can do in C that you can't do the same way in
Python.  And that's *by intention*.  It isn't a bad thing, it was done
for good design reasons, reasons well-tested by previous languages like
Lisp.

  If you really must do a "pointer-style thing" in Python, you can do
this, which is semantically equivalent to the C version:
>>> def incr(pointerToVar):
...     pointerToVar[0] += 1
...
>>> a = [0]
>>> b = a
>>> incr(a)
>>> a[0]
1
>>> b[0]
1
>>> incr(a)
>>> a[0]
2
>>> b[0]
2

  I hope no Pythonista would actually *do* that, but it's working code,
and the foo[0] is similar to explicit referencing and dereferencing of
pointers.

  Since every operation in Python operates on pointers, there's no use
in having a special syntax for it.  You don't need all the * and & and
-> line noise.

  Stop trying to make Python into C/C++, and you'll be happier with it.
Or stop using Python, if you really don't like the design philosophy.
There are plenty of Algol-derived languages out there.  PHP and
especially Perl are more C-like in their internal logic, and you might
find them more pleasant.

-- 
 <a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
"The computer is incredibly fast, accurate, and stupid.
 Man is unbelievably slow, inaccurate, and brilliant.
 The marriage of the two is a force beyond calculation." -Leo Cherne




More information about the Python-list mailing list