[Tutor] Keeping change-in-place vs. copy methods straight
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.
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?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor