A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2014-May/134629.html below:

[Python-Dev] Returning None from methods that mutate object state

[Python-Dev] Returning None from methods that mutate object stateTerry Reedy tjreedy at udel.edu
Wed May 21 01:56:37 CEST 2014
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

More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4