anything like C++ references?

Adam Ruth owski at hotmail.com
Wed Jul 16 18:00:22 EDT 2003


In <8sdbhvk5b5ia2onmbca4ajkaidpu3ht9ed at 4ax.com> Stephen Horne  wrote:
> On Wed, 16 Jul 2003 19:06:40 +0000 (UTC), Adam Ruth
> <owski at hotmail.com> wrote:
> 
>>In <mailman.1058377226.21359.python-list at python.org> Michael Chermside  
>>wrote:
> 
>>> Look, first of all, let me say that I disagree with Stephen Horne in
>>> this discussion. Or, to be more precise, I think that the approach 
>>> he is using is not one which is useful in describing Python. HOWEVER,
>>> that doesn't mean that there's NOTHING to what he is saying, and 
>>> your claim that there is no situation requiring "pointers" in Python 
>>> seems wrong to me.
> 
> Thankyou - nice to know I'm not *completely* insane ;-)
> 
>>You are correct in that there are times when pointer semantics would 
>>be  somewhat useful, but they're never necessary.  His statement was 
>>that  there are time that "you really can't avoid pointers".
> 
> Hmmm - those words were in a context. I never claimed that "you really
> can't avoid pointers" in *current* Python - that's obviously not true.
> But if the copy-on-write stuff were implemented, the need would arise.
> For example...

It sure seemed that way to me:

<prior dicussion>
>>They're an unhappy necessity, akin to stop-lights.  You need them
>>because roads intersect, but if roads don't intersect, don't use them!
>
>Absolutely true. And the worst thing you can do when you really can't
>avoid pointers is to obscure the issue even more by disguising them as
>something else.
</prior discussion>

We were talking about people who wrap things in lists to simulate 
pointers.  It would seem, from your statement, you think there are 
situations where you really can't avoid pointers.  If I misread that, I 
apologize.

> 
>>>> class c :
> ....   x = 0
> ....
>>>> k = c()
>>>> def fn(a) :
> ....   a.x=1
> ....
>>>> fn(k)
>>>> k.x
> 
> At the moment, the result is 1. With copy-on-write, object parameters
> would behave exactly the same as integer or other immutable
> parameters. The result would be 0. You'd need pointers or references
> or call-by-reference to do a number of things.

Something occurred to me earlier today.  And that is that Python does 
use copy-on-write semantics for parameter calls, it's just that Python 
only has one data type.  A reference.  The reference is passed by value 
to the function, just as you describe it should.  Python, however, has a 
futher layer of abstraction on top of its one data type, and that's 
objects.  I would venture that it's the extra layer of abstraction that 
makes Python work in a more intuitive, proper way.

This is, however, just my opinion.  But it does seem that new 
programmers who learn this abstraction find it natural and simple.  It's 
people coming from C, et al., that seem thrown off by it.

> 
>>I definitely went a little overboard, and it sounds like I'm saying, 
>>"not only are pointers not necessary, they're never desirable".  My 
>>tone  was a bit more knee jerk than was prudent.
> 
> Then I withdraw certain comments I've made. I really can't complain
> about people going "a little overboard", can I ;-)

It's much less overboard then I've been known to go.  I must be making 
progress :)

> 
>>This is just as clear as the C++ version, and more clear than the 
>>Python  version that wraps with the list.  This example, though, 
>>doesn't really show the difference, it's too  trivial.  All of the 
>>versions are clear enough, with the difference  being academic. 
> 
> I agree with both of these comments.
> 
>>I would be interested in seeing a more complex example where something 
>>would be substantially cleaner with pointers.  I have to acknowledge 
>>the  possibility that they exist, I don't know everything... yet :)
> 
> I was told today that both Perl and ML have something equivalent to
> pointers. I don't know either language, though. Given the current
> audience, mentioning Perl may be a mistake - but we could look up the
> rationale for including them in ML.
> 
> That is something to do with caution, though. I imagine that ML is a
> very different language to Python. I have used Haskell and Miranda,
> which are at least broadly the same paradigm but may or may not be
> quite a similar languages, but even those I never exactly knew well.
> 
> The rationales might not be very portable.

>From this discussion, there seem to be two points regarding pointers.

1)  Whether or not pointers are necessary, or helpful in Python as it 
now stands.  This is debatable.
2)  Whether or not pointers would be necessary if Python used a form of 
parameter passing more like you prefer.  I now see better that this is 
what you are talking about regarding pointers.  That they *would* be 
necessary if copy-on-write were in use.  That I can agree with, they 
would be necessary (or at least some of the semantics).  I still can't 
see that copy-on-write is better than what we have now, but hey, I 
prefer Pepsi to Coke, so that tells you something!

As a side note, based on your description of your eduction and studies 
in an earlier post, I realize now how different our backgrounds seem to 
be.  I'm coming from a world where the focus is engineering (Ada is the 
only language purpose-built for engineering, AFAIK).  You seem to come 
from a background more focused on science and theory.  Perhaps this is 
the cause of our different world views (on this issue).  Just a thought, 
though it is interesting we both chose Python.

Adam Ruth




More information about the Python-list mailing list