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

Cameron Simpson cs at zip.com.au
Tue Apr 29 00:27:11 CEST 2014

On 28Apr2014 14:45, taserian <taserian at gmail.com> wrote:
>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.
>Is there some sort of rule-of-thumb to determine if a function is in-place
>or returns a value?

In python, the convention is that a function that changes in place returns 
None. This avois people accidentally thinking they have a copy when in fact 
they have the original.

Of course, it is a work practice, so you need to consult the doco of any 
particular function. But it is a good practice to adopt in one's own code.

As you suggest, this keeps the two notions separate by making their use in the 
code distinct.

Cameron Simpson <cs at zip.com.au>

Too young to rest on the weekend, too old to rest during the week.
         - Mark Randol <ryvw50 at email.sps.mot.com>

More information about the Tutor mailing list