Pass-by-reference : Could a C#-like approach work in Python?

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Thu Sep 11 07:22:40 EDT 2003


On Wed, 10 Sep 2003 21:45:38 -0700, "Carl Waldbieser"
<waldbie at attglobal.net> wrote:

>I think you could still run into a problem here with aliasing.  For example:
>
>    obj = Obj_Type ()
>    x = obj
>
>    obj.Do_Setup_Stuff_1 ()
>    obj.Do_Setup_Stuff_2 ()
>    obj.Do_Setup_Stuff_3 ()
>    container.Add (ref obj) #rebinds obj to None
>    #later...
>    x.alter() #alters containers copy anyway

Yes - that's what I meant by "this is by no means foolproof of course"
in my post with message ID jnkulv8t6e1qc3mb4pmf0k706e98pgmss2 at 4ax.com.

>This kind o problem exists in other languages like C++, too.  The containers
>in the C++ standard library copy elements to side-step these problems.  When
>copying becomes expensive, pointers to elements are used instead, but since
>you can have multiple pointers to the same object, you run the risk of
>clobbering data in the container if you are not careful.

Its not unusual to use a reference counting scheme to restore the
apparant copy semantics while avoiding unnecessary copying, but of
course you are correct.

>In practice, though, this type of behavior has rarely been a problem for me.

Yes - if I trim down your tail quoting somewhat...

>> So my 'most compelling example' isn't compelling at all. Damn.
>>
>> I've been thinking on this for some hours now and still not come up
>> with a more compelling example (or at least not one which can't be
>> better handled a different way) so I guess the idea is a dud.
>>
>> Bet you never expected me to admit that so easy ;-)





More information about the Python-list mailing list