meaning of: line, =

Devin Jeanpierre jeanpierreda at gmail.com
Thu Feb 5 20:12:17 EST 2015


On Thu, Feb 5, 2015 at 8:08 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Thu, Feb 5, 2015 at 2:40 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> Devin Jeanpierre wrote:
>>> On Wed, Feb 4, 2015 at 1:18 PM, Chris Angelico <rosuav at gmail.com> wrote:
>>>>>>>> [result] = f()
>>>>>>>> result
>>>>> 42
>>>>
>>>> Huh, was not aware of that alternate syntax.
>>>
>>> Nor are most people. Nor is Python, in some places -- it seems like
>>> people forgot about it when writing some bits of the grammar.
>>
>> Got an example where you can use a,b but not [a,b] or (a,b)?
>
>>>> def f(a, (b, c)):
> ...     print a, b, c
> ...
>>>> f(3, [4, 5])
> 3 4 5
>>>> def g(a, [b, c]):
>   File "<stdin>", line 1
>     def g(a, [b, c]):
>              ^
> SyntaxError: invalid syntax
>
> Although to be fair, the first syntax there is no longer valid either
> in Python 3.

As Ian rightly understood, I was referring to differences between "[a,
b, ...]" and "(a, b, ...)".

Here's another example, one that still exists in Python 3:

>>> [] = ''
>>> () = ''
  File "<stdin>", line 1
SyntaxError: can't assign to ()

The syntax explicitly blacklists (), but forgets to blacklist [].

-- Devin



More information about the Python-list mailing list