[Python-Dev] Returning None from methods that mutate object state
Terry Reedy
tjreedy at udel.edu
Wed May 21 01:56:37 CEST 2014
More information about the Python-Dev mailing list
Wed May 21 01:56:37 CEST 2014
- Previous message: [Python-Dev] Returning None from methods that mutate object state
- Next message: [Python-Dev] Returning None from methods that mutate object state
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 5/20/2014 12:30 PM, Chris Barker wrote:
> >>>> [].sort() is None
> > True
> >>>> "ABC".lower() is None
> > False
> Is there a reference anywhere as to *why* the convention in Python is to
> do it that way?
In short, reducing bugs induced by mutation of aliased objects.
Functional languages evade the problem by prohibiting mutation
(sometimes at the cost of inefficiency).
In an alternate universe, the example above might become
>>> a = []; a.sort() is a
True
>>> a = "ABC"' a.lower() is a
False
As I suggested earlier, having pure mutation methods not return anything
made is easy to suggest a mutation + non-self return method, list.pop.
If all mutation methods had previously returned 'self', there might have
been disagreement over whether the item return should augment or replace
the self return. Before you say the latter, consider the inconsistency
of only sometimes returning self and the potential consistency between
>>> most, last = 'a b c'.rsplit(maxsplit=1)
>>> most, last
('a b', 'c')
>>> most, last = [0, 1, 2].pop()
>>> most, last
([0, 1], 2)
One could also consider first, rest pairings.
--
Terry Jan Reedy
- Previous message: [Python-Dev] Returning None from methods that mutate object state
- Next message: [Python-Dev] Returning None from methods that mutate object state
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list