anything like C++ references?

Stephen Horne intentionally at blank.co.uk
Tue Jul 15 17:14:20 EDT 2003


On 15 Jul 2003 20:32:30 GMT, bokr at oz.net (Bengt Richter) wrote:

>On Tue, 15 Jul 2003 02:40:27 +0100, Stephen Horne <intentionally at blank.co.uk> wrote:
>
>>On Mon, 14 Jul 2003 00:07:44 -0700, Erik Max Francis <max at alcyone.com>
>>wrote:
>>
>>>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.
>>
>>Not true.
>>
>>If you eliminate all violations of the idea of variables bound to
>>values (basically the whole point of my thread), then mutable
>>containers cannot achieve the same thing.
>>
>>If a copy-on-write scheme is added to Python, you'd get the following
>>results from using mutable containers...
>>
>>>>> a = [1, 2, 3]
>>>>> b = a
>>>>> b[2] = 4
>>>>> a
>>[1, 2, 3]
>>>>> b
>>[1, 2, 4]
>>
>Yes, but it would be hugely inefficient for the case where, e.g., you are
>modifying lines read from a 20MB log file. Imagine inducing a 20MB copy
>every time you wanted to delete or modify a line. Now you have to invent
>a way to program around what can be done very conveniently and efficiently
>thanks to Python's semantics.

What makes you say that!

Copies are only made when they are needed. The lazy copy optimisation,
in other words, still exists.

Delete or modify one string in a list of strings, and the same stuff
would happen as happens now. Unless, perhaps somewhere you don't know
about, the caller of your function who passed that list in to you has
a separate reference they expect to stay unchanged. In that case, the
first changed string triggers a copy of the list - but as the list
only contains references to strings, it doesn't trigger copying of all
the strings. The second line changed doesn't require the list to be
modified again because you already have your separate copy.

C++ uses exactly this kind of approach for the std::string class among
others. Copy-on-write is a pretty common transparent implementation
detail in 'heavy' classes, including those written by your everyday
programmers. Does that mean C++ is slower that Python? Of course not!





More information about the Python-list mailing list