Why list.sort() don't return the list reference instead of None?

A

ankyhe

L = [4,3,2,1]
L=L.sort()
L will refer to None, why L.sort() don't return the L?
I want to ask why the designer of Python do so?
 
E

Erik Max Francis

L = [4,3,2,1]
L=L.sort()
L will refer to None, why L.sort() don't return the L?
I want to ask why the designer of Python do so?

Because that's the convention that signifies that a Python method
mutates the object rather than returns a new one.
 
L

Lawrence Oluyede

I want to ask why the designer of Python do so?

I'm not a Python core developer nor a designer but I've always known that
sort() is a in-place sort and since the list is a mutable object it mutates the
list sending the "sort()" message. If you want to get back a sorted iterable
use... sorted :)

L = [3, 1, 2]
ls = sorted(L)

now ls is your list, sorted.
 
A

ankyhe

Thanks a lot!

However, I wonder why L.sort() don't return the reference L, the
performance of return L and None may be the same. If L.sort() return
L, we shouldn't do the awful things such as:
keys = dict.keys()
keys.sort()
for key in keys:
...do whatever with dict[key]...
we can only write the code as follows:
for key in dict.iterkeys().sort():
...do whatever with dict[key]...

Why?
 
L

Lawrence Oluyede

However, I wonder why L.sort() don't return the reference L, the
performance of return L and None may be the same.

It's not "the same". sort() does not return anything.

I've just explained to you and so the others: by default operations on mutable
objects are in place.

s = "abc"
s.upper()

does return another string. String are immutable references.
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
However, I wonder why L.sort() don't return the reference L, the
performance of return L and None may be the same.

It's probably because it would become confusing. Many people don't
read the documentation. If L.sort() returns a sorted version of L,
they would probably assume it didn't do an in-place sort. The effects
of an unexpected in-place sort are much harder to track down and debug
than a function returning None.

Sybren
 
T

Tim N. van der Leeuw

So you write:

for key in sorted(dict.iterkeys()):
... do it ...

dict.iterkeys() returns an iterable which doesn't even have a
sort-method; and somehow I find it unnatural to apply a 'sort' method
to an iterator whereas I find it perfectly natural to feed an iterator
to a function that does sorting for anything iterable...

Cheers,

--Tim
 
V

vbgunz

to throw fire on the fuel :)P), you can get the value back to an
in-place mutable change with a single expression...

mylist = [2,3,4,1]
print mylist.sort() or mylist

might not be too pythonic or maybe it is. I guess depends on what side
of the glass you might wish to view the solution :)
 
B

bruno at modulix

Lawrence said:
It's not "the same". sort() does not return anything.

Yes it does : it returns the None object.
I've just explained to you and so the others: by default operations on mutable
objects are in place.

this is pure non-sens :

class MyList(list):
def sort(self):
return sorted(self)

This is a mutable object, and the sort() is not in place.

class MyObj(object):
def __init__(self, name):
self.name = name

def sayHello(self):
return "hello from %s" self.name

This is another mutable object, and I fail to see how 'in place' could
sensibly have any meaning when applied to sayHello().

Also, and FWIW, the fact that a method modifies the object it's called
on doesn't technically prevent it from returning the object:

class MyOtherList(list):
def sort(self, *args, **kw):
list.sort(self, *args, **kw)
return self
s = "abc"
s.upper()

does return another string. String are immutable references.

Strings are immutable *objects*.
 
B

bruno at modulix

vbgunz said:
to throw fire on the fuel :)P), you can get the value back to an
in-place mutable change with a single expression...

mylist = [2,3,4,1]
print mylist.sort() or mylist

might not be too pythonic or maybe it is. I guess depends on what side
of the glass you might wish to view the solution :)

Anyway, please do us a favor : avoid using such a thing in production
code !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top