[Python-ideas] Unpacking iterables for augmented assignment

Kirill Balunov kirillbalunov at gmail.com
Sun Aug 26 14:48:59 EDT 2018


вс, 26 авг. 2018 г. в 14:53, Stephen J. Turnbull <
turnbull.stephen.fw at u.tsukuba.ac.jp>:

> [...]
>
> This isn't consistent with the normal behavior of Python sequences:
>
> $ python3.6
> >>> (1,2) + (3,4)
> (1, 2, 3, 4)
>
> That is, "+" means something different for sequences.  Furthermore,
> the thing on the LHS is tuple syntax.  There's only one reasonable
> meaning to give to an ordinary assignment from a sequence on the RHS
> to a "tuple of variables".
>

What do you mean by "consistent with the normal behavior of Python
sequences"? Personally, I do not see any consistency. Currently, in Python
3.7:

>>> a = [ 1, 2, 3]
>>> b = ( 1, 2, 3)

>>> a + b
...
TypeError: can only concatenate list (not "tuple") to list


But:

>>> a += b
[1, 2, 3, 1, 2, 3]

As for me, Python has several historical artifacts (warts) which can not be
fixed by now. I'm sure that someone, on the contrary, finds them as
advantages (and I'm not interested that someone started to convince me in
the opposite). Among them are: reversed order of arguments in
`enumerate`, the absence of literals for `set` and `frozenset` types, all
`bytes` story in Python 3, `+` operator overload for sequences and others.

But for this particular case, `+` operator overload for concatenation, I
think Python can do better and have the opportunity. Especially, taking
into account the switch to generators for builtins in Python 3 and that in
30-40% times, rough estimation, `+` operator for sequences is used
for throw away concatenation with unnecessary usage of resources in some
cases. I like the way this problem was addressed and solved in Coconut
language <http://coconut-lang.org/> with `::` operator. Coconut uses the `
::` operator for iterator chaining. Since Coconut is just a transpiler, it
is the best they can do. I don't think that just wrapping sequences and
iterators with chain is the right solution for Python and also it is not
enough to introduce new operator into the language. I would like to see it
in a generalized form, for example with the possibility to access items
with slices and indices,.. .I understand that this is not so easy, but at
least I think that this is the right direction for several reasons: it give
a possibility for future pattern matching syntax/functionality in Python,
it can be done lazily and,  in my opinion, `+` operator overload for
builtin sequence types is a historical design mistake.

with kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180826/237e9fde/attachment-0001.html>


More information about the Python-ideas mailing list