osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)


On Sun, Jun 24, 2018 at 05:24:12PM +0300, Ivan Pozdeev via Python-Dev wrote:

> An expression is intuitively thought to be self-contained i.e. without 
> side effects.
> if I write `a=b+1`, I'm not expecting it to do anything except assigning 
> `a'.

a = d.pop(1)
a = d.setdefault(key, 0)
chars_written = file.write(text)


> Expressions with side effects has long since proven to be problematic 
> because of the implicit (thus hard to see and track) links they create
> (and because the result depends on the order of evaluation).

If you're going to take a hard-core functional approach to side-effects, 
I think you are using the wrong language. Nearly everything in Python 
*could* have side-effects (even if usually it won't).

Even your own example of "b+1" (depending on what b.__add__ does).


> Moreover, Python's other design elements have been consistently 
> discouraging expressions with side effects, too (e.g. mutator methods 
> intentionally return None instead of the new value, making them useless 
> in expressions), 

I don't think that's the reason why mutator methods return None. They 
return None rather than self to avoid confusion over whether they return 
a copy or not.

https://docs.python.org/3/faq/design.html#why-doesn-t-list-sort-return-the-sorted-list


> so the proposition is in direct conflict with the 
> language's design.

Python is full of operations with side-effects.

Besides, they're not quite useless:

    (alist.append() or alist)

is functionally equivalent to alist.append returning self. Just a bit 
more verbose.

Methods (and functions) all return a value, even if that value is None, 
so they can be used in expressions. If Guido wanted Pascal style 
procedures, which cannot be used in expressions, we would have them by 
now :-)


-- 
Steve