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

bruno at modulix onurb at xiludom.gro
Tue May 9 08:42:06 EDT 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.

> 
>>Why?
>  
> 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*.




-- 
bruno desthuilliers
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 mailing list