osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A question on modification of a list via a function invocation


On 5 Sep 2017 14:28:44, wlfraed at ix.netcom.com (Dennis Lee Bier) wrote: 
> On 5 Sep 2017 17:57:18 GMT, <a onclick="addTo('ram at zedat.fu-berlin.de')"
href="https://webmail.psu.edu/webmail/retrieve.cgi?mailbox=inbox&start_num=14200&limit=50&sort=0&display=4&timestamp=20170906045729&mid=mailman%2e1111%2e1504662834%2e2732%2epython%2dlist%40python%2eorg#";>ram at zedat.fu-berlin.de</a> (Stefan Ram) 
> declaimed the following:

>>  But what does "a C++ reference" refer to?
>>

> Per Stroustrup (The C++ Programming Language 4th Ed, page 189)

> """
> * ...> * A reference always refers to the object to which it was initialized.
> * ...

> A reference is an alternative name for an object, an alias. ...
> """

> {Hmmm, and I see that the syntax can be used outside of parameter
> declaration -- which is the only place I'd seen it previously... either
> this is a change from earlier standards, or my classes just didn't feel the
> need to expose a non-parameter reference -- since, based upon the above
> book, you can not declare a bare reference "variable"; it MUST be
> initialized with a real object.}

I think I can say something about this, having been a teacherof the classes you refer to.   I intentionally avoided reference variables.
IMO, the 'good' use for declaring a new reference variable (i.e. not parameter)would be when (1) the object to which you refer to is time-consuming to access(2) you plan to refer to this object more then once, and don't want to repeatthat time-consuming process, and (3) you really want a reference, and not a copy.
The first two years of programming courses really do not have a purposethat meets all three, so can "didn't feel the need" is probably applicable.
I intentionally avoided them because reference variables simply compoundthe problem of aliasing, so unless you really limit your reference variableto a very tight sandbox, you could be causing more headache than you save.
I do admit to occasionally defining a method that returned a reference,such as one that overloads the [] operator.   But even so, I would generallybe reluctant to giving an outside client a direct access to my database'sinternal structures.  (Thank you Python for separating __getitem__ and __setitem__)
Python doesn't eliminate aliasing, of course, since most assignment operationscreate aliases.  But at least it's nice to know that aliasing immutable valuesis harmless.   Hence my unit on building recursive data structures entirelyout of tuples.
Roger ChristmanPennsylvania Sate University