I've noticed that a lot of the standard Python methods on lists and dictionaries don't return anything at all – they simply modify self and return nothing.

>>> l = [1,2,3]
>>> r = l.reverse()
>>> print l
[3, 2, 1]
>>> print r
None

This is becoming extremely frustrating as it prevents chaining of method calls, which would be possible if only self was returned. This keeps catching me out if I attempt to return some_list.reverse() or dict = {'foo':bar, 'x':y}.update(some_other_dict) as I end up with None when I expected Something.

It's the little details that make a language a joy or a chore.

2 Comments

  1. Thanks for pointing me to the original wisdom that guides this API design choice. I can see the logic, but I don’t find it any less frustrating that r = l.reverse() doesn’t assign anything to r.

Leave a Reply

Your email address will not be published. Required fields are marked *