Why return None?

Benjamin Niemann b.niemann at betternet.de
Wed Aug 25 12:45:46 CEST 2004

Martin DeMello wrote:

> Anthony Baxter <anthonybaxter at gmail.com> wrote:
>>On Wed, 25 Aug 2004 08:26:26 GMT, Martin DeMello
>><martindemello at yahoo.com> wrote:
>>>It seems to be a fairly common pattern for an object-modifying method to
>>>return None - however, this is often quite inconvenient.
>>list.reverse() modifies the list in place. The python idiom is that
>>these don't return a reference to the modified list. Although note the
> Yes, but why? I mean, is there either an advantage to returning None or
> some inherent danger in returning self?
The danger is that you might forget about the side effect.

l = [1, 2, 4, 3]
sorted = l.sort()
... # do something with sorted list
print l   # I want to unsorted list here, but oops...

In place object modification is often faster, but you loose the original 
data - this should be explicitly visible in the code

More information about the Python-list mailing list