[ python-Bugs-1743665 ] conditional expressions vs. () and * in call arguments.
SourceForge.net
noreply at sourceforge.net
Tue Jun 26 18:51:26 CEST 2007
Bugs item #1743665, was opened at 2007-06-26 15:18
Message generated for change (Comment added) made by gbrandl
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1743665&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: Python 2.5
>Status: Closed
>Resolution: Invalid
Priority: 5
Private: No
Submitted By: Mike Meyer (mwm)
Assigned to: Nobody/Anonymous (nobody)
Summary: conditional expressions vs. () and * in call arguments.
Initial Comment:
Consider:
>>> def foo(x, y=23):
... print x, y
...
>>> data = (1, 2)
>>> foo(*data if data else data)
1 2
>>> data = None
>>> foo(*data if data else data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() argument after * must be a sequence
>>>
Ok, * binds tighter than if else, so add the parens to fix this:
>>> foo((*data) if data else data)
File "<stdin>", line 1
foo((*data) if data else data)
^
SyntaxError: invalid syntax
>>>
The parser thinks I'm trying to use the *tuple syntax inside a tuple
in an argument list - at least, that's what I think it is flagging as
invalid syntax. But that's not what I'm doing! Wrapping parens around
an expression doesn't automatically turn it into a tuple, even in an
argument list:
>>> foo((1))
1 23
>>>
This looks like a parser bug to me. However, the interactions of the
various things one can put in argument lists have gotten complex
enough that it's hard to say for sure. If it's not a bug, it should
probably be documented somewhere.
For the record, the workaround is:
>>> foo(*data if data else (data,))
None 23
>>>
And yes, that last comma is required, otherwise you're back to the
first error.
----------------------------------------------------------------------
>Comment By: Georg Brandl (gbrandl)
Date: 2007-06-26 16:51
Message:
Logged In: YES
user_id=849994
Originator: NO
Sorry, I can't see a problem here.
"data if data else data" is nonsensical and equivalent to "data". This of
course doesn't work since None is not a sequence.
Thus, your "workaround" is the correct solution.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1743665&group_id=5470
More information about the Python-bugs-list
mailing list