Why return None?

Martin DeMello martindemello at yahoo.com
Wed Aug 25 15:30:14 EDT 2004


Dave Opstad <dave.opstad at agfamonotype.com> wrote:
> 
> But isn't the fact that the list is modified in place incidental to the 
> fact of sorting? One is an implementation detail, and the other is the 
> semantic meaning you're trying to express.

Nope, especially since objects are passed by reference. 

> In my opinion, it would make more sense to have:
> 
> [1, 3, 4, 2].sort() return [1, 2, 3, 4]
> (1, 3, 4, 2).sort() return (1, 2, 3, 4)
> '1342'.sort() return '1234'
> 
> and so on. As it is, we have sort working on lists but not on immutable 
> sequences, which is inconvenient at times.

This again would be a difference between sort() and sorted().

My main point is that returning None is pretty useless - it 'wastes' the
return value, and doesn't allow method chaining. Returning self allows
you to either use or discard the return value depending on whether
you're interested in it, whereas using None doesn't give you any choice.

martin



More information about the Python-list mailing list