[Tutor] Keeping change-in-place vs. copy methods straight

taserian taserian at gmail.com
Mon Apr 28 20:45:47 CEST 2014


I can't claim to be new to programming, but I've dabbled in Python over and
over again to get small problems and puzzles resolved. One thing that I
find I can't keep straight are the methods that change a list in place, vs.
those that return a copy (sometimes transformed) of the list.

Call me old-fashioned, but my programming experience mostly comes from
languages where you assigned the output of a function to another variable,
so you always had a copy of whatever you were working on.

    var array;
    sorted = array.sort();

If you didn't care to keep both copies, you could always re-assign the
returned value to the original variable.

    array = array.sort();

If I try to do the same in Python:

    sorted = arrayList.sort()

sorted comes back as None, while arrayList has changed its order.

Is there some sort of rule-of-thumb to determine if a function is in-place
or returns a value?

Antonio Rodriguez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140428/3a79386f/attachment.html>


More information about the Tutor mailing list