Why list.sort() don't return the list reference instead of None?
bruno at modulix
onurb at xiludom.gro
Tue May 9 14:42:06 CEST 2006
Lawrence Oluyede wrote:
> "ankyhe at gmail.com" <ankyhe at gmail.com> writes:
>>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.
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 :
This is a mutable object, and the sort() is not in place.
def __init__(self, name):
self.name = name
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:
def sort(self, *args, **kw):
list.sort(self, *args, **kw)
> s = "abc"
> does return another string. String are immutable references.
Strings are immutable *objects*.
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list