List mutation method gotcha - How well known?

Arnaud Delobelle arnodel at googlemail.com
Thu Mar 13 15:13:19 EDT 2008


On Mar 13, 10:42 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
[...]
> By Python convention, methods that mutate the object return None, and
> also stuff that returns None doesn't generate output at the
> interactive prompt.

A convention that does not always hold:

>>> l = [1, 2, 3]
>>> l.pop()
3
>>> l
[1, 2]
>>>

There is also the .next() method:

>>> i = iter([1, 2, 3])
>>> i.next()
1
>>> i.next()
2
>>>

I can't think of others ATM in python 2.x but there might be some.

Moreover PEP 3119 introduces .add(), .discard(), .toggle() for
MutableSets which all mutate self and return a non None object.

--
Arnaud




More information about the Python-list mailing list