[Python-Dev] (no subject)

Donald Stufft donald at stufft.io
Tue Feb 10 01:12:33 CET 2015


> On Feb 9, 2015, at 4:06 PM, Neil Girdhar <mistersheik at gmail.com> wrote:
> 
> Hello all,
> 
> The updated PEP 448 (https://www.python.org/dev/peps/pep-0448/ <https://www.python.org/dev/peps/pep-0448/>) is implemented now based on some early work by Thomas Wouters (in 2008) and Florian Hahn (2013) and recently completed by Joshua Landau and me.
> 
> The issue tracker http://bugs.python.org/issue2292 <http://bugs.python.org/issue2292>  has  a working patch.  Would someone be able to review it?
> 

I just skimmed over the PEP and it seems like it’s trying to solve a few different things:

* Making it easy to combine multiple lists and additional positional args into a function call
* Making it easy to combine multiple dicts and additional keyword args into a functional call
* Making it easy to do a single level of nested iterable "flatten".

Looking at the syntax in the PEP I had a hard time detangling what exactly it was doing even with reading the PEP itself. I wonder if there isn’t a way to combine simpler more generic things to get the same outcome.

Looking at the "Making it easy to combine multiple lists and additional positional args into a  function call" aspect of this, why is:

print(*[1], *[2], 3) better than print(*[1] + [2] + [3])?

That's already doable in Python right now and doesn't require anything new to handle it.

Looking at the "making it easy to do a single level of nsted iterable 'flatten'"" aspect of this, the example of:

>>> ranges = [range(i) for i in range(5)]
>>> [*item for item in ranges]
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]

Conceptually a list comprehension like [thing for item in iterable] can be mapped to a for loop like this:

result = []
for item in iterable:
    result.append(thing)

However [*item for item in ranges] is mapped more to something like this:

result = []
for item in iterable:
    result.extend(*item)

I feel like switching list comprehensions from append to extend just because of a * is really confusing and it acts differently than if you just did *item outside of a list comprehension. I feel like the itertools.chain() way of doing this is *much* clearer.

Finally there's the "make it easy to combine multiple dicts into a function call" aspect of this. This I think is the biggest thing that this PEP actually adds, however I think it goes around it the wrong way. Sadly there is nothing like [1] + [2] for dictionaries. The closest thing is:

kwargs = dict1.copy()
kwargs.update(dict2)
func(**kwargs)

So what I wonder is if this PEP wouldn't be better off just using the existing methods for doing the kinds of things that I pointed out above, and instead defining + or | or some other symbol for something similar to [1] + [2] but for dictionaries. This would mean that you could simply do:

func(**dict1 | dict(y=1) | dict2)

instead of

dict(**{'x': 1}, y=2, **{'z': 3})

I feel like not only does this genericize way better but it limits the impact and new syntax being added to Python and is a ton more readable.


---
Donald Stufft
PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150209/f1feaca9/attachment.html>


More information about the Python-Dev mailing list