[Tutor] 'in-place' methods
Orri Ganel
singingxduck at gmail.com
Fri Feb 17 23:30:48 CET 2006
Michael Broe wrote:
>I think I understand this sorting-a-list 'in place' stuff, and things
>of that kind (reversing for example); but I am finding it very
>difficult to get used to, since sorting a list doesn't return the
>sorted list as a value, but simply does the work as a side effect.
>
>The place where it really trips me up is when I want to do something
>like pass a sorted list as a value in a program, say.
>
>Here is a trivial example. Say I want to define a function to always
>print lists in sorted form. I can't do it as follows, tho it's what I
>want to do intuitively:
>
>def sort_print(L):
> print L.sort()
>
>This is on a strict analogy to writing a function that prints strings
>in upper case only:
>
>def upper_print(s):
> print s.upper()
>
>(The fact that the second works and the first doesn't really does bug
>me as a newbie.)
>
>Anyway, first question: is the fact that the first doesn't work
>purely a function of the fact that lists are mutable? (At least I
>could kind of understand that, that methods work differently for
>objects of different types). Or is it possible to have methods
>like .upper() that return the result of performing the operation even
>for lists?
>
>Second question. Do I really have to write the sort_print() function
>like this:
>
>def sort_print(L):
> L.sort()
> print L
>
>i.e. first perform the operation in-place, then pass the variable? Is
>this the idiomatic way of doing it?
>
>Third question: is this in-place behavior of methods in effect
>restricted to lists, or are there other places I should be on the
>lookout for it?
>
>Cheers,
>Mike
>
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
Well, to answer the specific case of l.sort(), there's a built-in that
returns a sorted list - sorted(l). So to do what you want, you'd need:
>>> import random # as you can see, not
all operations have
>>> examplelist = random.sample(range(10), 10) # non-in-place
counterparts, or i would have
>>> print sorted(examplelist) # used
random.shuffle(range(10))
[1,2,3,4,5,6,7,8,9]
Except for a few specific cases, to use a non-in-place version of an
in-place operation, you'll probably have to end up writing your own,
similar to the way you currently deal with l.sort(). As for why this
is, I think you've hit on the answer: the default, if you will, is to
perform operations in-place, to avoid the necessity of reassigning a
list to it's sorted version, to give one example. However, this only
works on mutable objects - you can't modify a string, because, well,
strs are immutable, so performing an operation in-place is nonsense.
That's why s.upper() returns a value, as opposed to modifying s.
To answer your second question, in general, yes. If there isn't already
a way to do it in the library, you'll have to write your own function.
Btw, another in-place list operation with a non-in-place counterpart is
l.reverse(), which has a few: reversed(l) and l[::-1] come to mind.
HTH,
Orri
--
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.
More information about the Tutor
mailing list