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

Alan Gauld alan.gauld at btinternet.com
Sun May 4 11:00:08 CEST 2014


On 28/04/14 19:45, taserian 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.

Join the club, it's (IMHO) one of the worst features of Python, you just 
have to get used to it. For the specific case of sort you can always use 
the sorted() function which does return a reference
(not a copy!) to the sorted item.

> 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.

The problem is if you expect a copy but actually its the original item 
that gets sorted what is returned is a reference. So you then have to 
work out if what you get back is a reference to the original or a new copy.

And the problem with copies is that if you are dealing with very big 
data sets holding two copies in memory might not be possible and at
the very least would be wasteful of resources.

>      sorted = arrayList.sort()
>
> sorted comes back as None, while arrayList has changed its order.

Yes, and it's often inconvenient to do it over two lines. That's why 
sorted() was introduced:

result = sorted(myList)

And it works with most sortable items including dictionaries
(based on the keys unless you specify otherwise)

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

Nope, read the documentation.
Fortunately in Python that's usually as simple as typing help(func)
at the >>> prompt.

The other area where there are lots of slightly weird inconsistencies 
are the set operations. There are symbol versions and function
versions of the same operation that behave slightly differently.
You need to watch out for the differences and use the correct
version. (The reasons for the differences are clear if you think about 
it but its still annoying to have such discrepancies.)


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list