Why return None?

Max M maxm at mxm.dk
Wed Aug 25 05:10:47 EDT 2004


Martin DeMello wrote:

> It seems to be a fairly common pattern for an object-modifying method to
> return None - however, this is often quite inconvenient.


With newstyle classe you can subclass the list class and make some 
functions return self.

class List(list):

     def sort(self):
         list.sort(self)
         return self


l = List([1,2,4,2,5,2,6])
print l.sort()

 >>>[1, 2, 2, 2, 4, 5, 6]

Well ok. Sort is a bad example, as you could just use sorted() insted.


regards Max M



More information about the Python-list mailing list