[Python-Dev] Returning None from methods that mutate object state
Chris Barker
chris.barker at noaa.gov
Thu May 22 01:33:54 CEST 2014
Thanks all,
Now I need to try to sum this all up to present to my students. ;-)
-Chris
On Tue, May 20, 2014 at 4:56 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> 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
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> chris.barker%40noaa.gov
>
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140521/ef559359/attachment.html>
More information about the Python-Dev
mailing list