By value or by reference?

Jacek Generowicz jacek.generowicz at cern.ch
Wed Oct 20 12:48:50 CEST 2004


aleaxit at yahoo.com (Alex Martelli) writes:

> Jacek Generowicz <jacek.generowicz at cern.ch> wrote:
>    ...
> > > > def doesThisLanguageBindByValue(language):
> > > >     a = 'original'
> > > >     b = a
> > > >     b = 'new'
> > > >     if a == 'original':
> > > >         print "%s assigns by value" % (language)
> > > >     else:
> > > >         print "%s does not assigns by value" % (language)
>    ...
> > > Can you give some examples of languages which would end up in the else
> > > branch?
> > 
> > Sure. Here it is in C++:
>    ...
> >   std::string  a = "original";
> >   std::string& b = a;
> 
> Ah, references, of course.

Well, the original question _was_ (I prapharse) "by *reference*, or by
value?". Given that C++ allows you to choose, why not choose?

> And you need to make the two "b =" bits drastically different in
> meaning,

_I_ don't. That C++ choses to do so, is a different matter. (Hence my
earlier comment about C++ doing exactly what it says on the tin being
rather rare :-)

> taking advantage of the fact that C++ overloads = to mean both
> initialization/construction and assignment; that's not quite
> cheating but it sure bends the rules;-).

I'm just using the surface syntax provided by C++. Don't blame me for
C++ being a complete mess :-)

> And after all this, the message "C++ does not assign by value" is false,
> because it sure does, under many (most!-) circumstances.

The relative frequency is a function of how often the programmer
choses to use "&".

> You'd need to rephrase this as "under at least some circumstances,
> C++ does assign not-by-value" or the like.

Which is why I _did_ rephrase the message in the C++ example :-0

> OK, so there are languages which have reference-variables

Exactly. And Python is not one of them.



More information about the Python-list mailing list