New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing *-unpacking generalizations #46545
Comments
The attached patch adds the missing *-unpacking generalizations.
>>> *a, b, c = a, b, *c
>>> a, b, c
([0, 1, 2], 3, 4)
>>> [ *a, b, c ]
[0, 1, 2, 3, 4]
>>> L = [ a, (3, 4), {5}, {6: None}, (i for i in range(7, 10)) ]
>>> [ *item for item in L ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Also, yielding everything from an iterator: >>> def flatten(iterables):
... for it in iterables:
... yield *it
...
>>> L = [ a, (3, 4), {5}, {6: None}, (i for i in range(7, 10)) ]
>>> flatten(L)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
This was discussed years ago and never got enough support: http://mail.python.org/pipermail/python-dev/2005-October/057177.html |
Didn't you say it does sets too? Does this work? How about dicts? Also, now that we support [*a, b, c] shouldn't we also support foo(*a, b, c) ? |
On Sat, Mar 15, 2008 at 9:12 AM, Guido van Rossum <report@bugs.python.org>
Yes.
Not yet.
Sure. (And also 'foo(*a, *b, *c)'?) But have you taken a look lately at the |
Looking at the flatten() example I'm curious -- how come the output of
is displayed as a list rather than as <generator at xxxxxx> ? Also, do I understand correctly that yield *(1, 2, 3) is equivalent to yield 1 ? (That's really cool.) |
On Sat, Mar 15, 2008 at 9:18 AM, Guido van Rossum <report@bugs.python.org>
It's a typo. It should've been list(flatten(L)) :-) (see the tests included |
It looks like I completely missed PEP-3132. Sorry for a misleading At least I am not alone: "A little new feature in Python 3 that many |
Just a nit: the syntax error message for invalid starred expressions |
This is fun, but needs more work (see python-3000 thread). I'm setting the priority to low, since I won't hold up a release to get |
Updated patch: reworked some internals, and added generalization of (The opcodes are largely unaffected; just the order of '*args' and This is still Work In Progress. To do: implement the dict unpacking Bzr branch for this patch is |
What's dict unpacking? I hope it's not an implementation of this sad {'a': x, 'b': y} = {'a': 42, 'b': 'hello'} # Same as x, y = 42, 'hello' :-) |
No, it's what you asked for in msg63551:
(unpacking of dicts in dicts.) |
I think we should either get this into the 3.0a5 release planned for May |
Agreed. A PEP was already on my TODO list (although I don't mind if |
Thomas, Could you add BUILD_UNPACK opcodes documentation to Do I understand correctly that non-starred arguments are packed into |
Can you show an example where this would be different? |
On Mon, Apr 7, 2008 at 9:00 PM, Alexander Belopolsky <report@bugs.python.org>
They don't interact. They're separate opcodes. The CALL_FUNCTION_* opcodes Updating the docs is on the TODO list.
Yes, that's what happens, but only in the presence of *args. For |
On Mon, Apr 7, 2008 at 3:07 PM, Guido van Rossum <report@bugs.python.org> wrote:
Admittedly a contrived example, but ... def __hash__(self):
... print('hash', self)
... return int.__hash__(self)
...
>>> a,b,c = map(X, range(3))
>>> {a,b,c}
hash 2
hash 1
hash 0
{0, 1, 2}
>>> {a,*(b,c)}
hash 0
hash 1
hash 2
{0, 1, 2} |
I missed the first line copying the class definition into my previous class X(int):
def __hash__(self):
print('hash', self)
return int.__hash__(self) |
I'm not sure how this matters. The order of evaluation is the same, the |
I agree with Thomas. The order in which __hash__() is evaluated |
On Mon, Apr 7, 2008 at 4:02 PM, Thomas Wouters <report@bugs.python.org> wrote:
I agree and that's why I said the difference in behavior is "subtle" |
I don't think the order in which the items are hashed is really what My patch actually includes pretty much the same change to BUILD_MAP, |
What's the status of this? Is it still under consideration for 3.0 -- if |
I was hoping this would make 3.1. Too late, I guess. What about 3.2? |
Removed dead code. Awaiting code review! :) |
All tests pass on my ubuntu 13.04 system. |
Removed a confusing and outdated comment in Lib/importlib/_bootstrap.py |
I have limited expertise in most of these areas, but I looked at starunpack40.diff and have these comments:
Otherwise, I didn't see anything that particularly scared me. Since we apparently don't have anyone willing and with the expertise to thoroughly check the patch, I'd vote for checking it in asap so it has more releases to bake before 3.5 final. |
Hi Steve: I have limited expertise in most of these areas, but I looked at starunpack40.diff and have these comments:
There was some code taken common in various places, but there should be no code for unpacking comprehensions left in. Do you have a question about a specific line?
They weren't added. They were moved by someone else. The only changes are exposing a method.
Good idea. I'll make that change.
Otherwise, I didn't see anything that particularly scared me. Since we apparently don't have anyone willing and with the expertise to thoroughly check the patch, I'd vote for checking it in asap so it has more releases to bake before 3.5 final. Thanks! |
All tests pass. All reviewer comments addressed. Please let me know if anything else needs to be done from my end. |
The latest patch looks good to me. No need to do the additional AST refactoring if it's going to make PEP-492's implementor's life harder (but I do read Guido's comment as a reason to check this in sooner rather than later :>) So, unless anyone objects I'll check in the latest patch on Monday. |
Thanks for the review Thomas! And yes, that's what I meant. :-) |
It certainly would be nice to have documentation. |
Yeah, but the docs don't need to be committed in time for beta 1. The On Sat, May 2, 2015 at 6:52 PM, Benjamin Peterson <report@bugs.python.org>
|
(To clarify, the PEP itself probably serves as enough documentation in the On Tue, May 5, 2015 at 4:47 PM, Guido van Rossum <guido@python.org> wrote:
|
On Tue, May 5, 2015, at 19:48, Guido van Rossum wrote:
I suppose I can just do it. |
a65f685ba8c0 |
Thanks Benjamin!
|
FYI, I meant last Monday, but I forgot it was May 4th (Dutch Memorial day) and May 5th (Dutch Liberation day), so that got in the way :P Should we keep this bug open for docs changes, or is there a separate issue for that? |
On Wed, May 6, 2015, at 09:15, Thomas Wouters wrote:
|
I get a test failure in Cython's compatibility tests which seems to be attributable to this change: """
>>> def sideeffect(x):
... L.append(x)
... return x
>>> def unhashable(x):
... L.append(x)
... return [x]
>>> L = []
>>> {1:2, sideeffect(2): 3, 3: 4, unhashable(4): 5, sideeffect(5): 6} # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: ...unhashable...
>>> L
[2, 4]
""" Instead, L ends up being [2, 4, 5]. Is this intended? Or acceptable? |
There is a change as part of this to make dict building more like list and set building, which both have this behaviour. The same changes have likely occurred before whenever BUILD_LIST and BUILD_SET were introduced, and this behaviour seems particularly undefined. That said, I did overlook the difference. Hopefully there's agreement that it doesn't matter. |
I think it's fine. It collects all the keys and values and then calls On Thu, May 7, 2015 at 11:40 AM, Joshua Landau <report@bugs.python.org>
|
This could likely stand to be clarified in the language reference, though |
Do we need to make lib2to3 compatible with the new grammar? |
Yes we should. I'd consider it a bug if it wasn't supported in 3.5.0 and we could fix that bug in 3.5.1. |
I created bpo-25969 to track the lib2to3 update. |
10 months and 2 releases after the code patch, the doc patches in bpo-24136 are incomplete (I believe), uncommitted, untouched since July, and forgotten about. The issue needs more help. |
bpo-26213 was opened for documenting bytecode changes. But 21 months and 3 releases after the code patch the documentation is still not updated. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: