Why return None?
Peter Otten
__peter__ at web.de
Wed Aug 25 06:43:04 EDT 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?
>
> martin
I think Guido would rather have newbies stumbling over
>>> a = list("abc")
>>> zip(a, a.reverse())
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: zip argument #2 must support iteration
than
>>> class List(list):
... def reverse(self):
... list.reverse(self)
... return self
...
>>> a = List("abc")
>>> zip(a, a.reverse())
[('c', 'c'), ('b', 'b'), ('a', 'a')]
The latter is more likely to remain undetected until some damage is done.
Still, I would prefer it.
Peter
More information about the Python-list
mailing list