anything like C++ references?

Tom Plunket tomas at fancy.org
Sun Jul 13 09:39:01 CEST 2003


Ian Bicking wrote:

> To be more specific, you would achieve the same effect with:
> 
> def change(val):
>     return val + 1
> i = change(i)

Yeah, thanks.  I don't recall off-hand why it is that I wanted
this functionality, but something came up a few weeks ago that
seemed to beg for it.  This was the solution that I used, but it
was non-optimal for some reason.  The question came to mind
because of that global question posted just previously, to which
I made a good response if I may say so myself.  :)

> There is no direct Python equivalent to the C++ function, for all 
> sorts of reasons (most of them very deliberate).

Ok.  I'd love to see the reasons for it.  To me it just seems
like there's a somewhat arbitrary line between immutables and
mutables.

> Is there someplace in particular to reference people who need to 
> learn about what Python variables are (that they are bindings, 
> etc)?

Yeah, this is actually less a stumbling block, and more a desire
to want to bind to another variable instead of that variable's
value.  I mean, if I do this:

a = [ ]
b = a
a.append(4)

print b
[ 4 ]

This seems inconsistent to me, although I understand that the two
variables are just binding to the same list.  No different than
binding to the same tuple or integer or string, it's just that
changing the value of a tuple, an integer, or a string means
rebinding your variable to a different object.

I suppose one "solution" would be a class that wraps the value,
so the value can change but the object stays the same.  Similar
is using a list, but these two options make it a little less
convenient, and one must exercise extreme caution lest one
accidentally reassign as an int instead of changing the internals
of the wrapper object.

> This question comes up in many forms all the time, particularly 
> from people with a C background.  Or rather, there are many 
> questions all of which are answered by that explanation...

IMHO the variable binding discussion doesn't answer the question,
it just provokes more questions.  :)

thanks to all who responded.

-tom!




More information about the Python-list mailing list