Can't do a multiline assignment!

s0suk3 at gmail.com s0suk3 at gmail.com
Thu Apr 17 13:26:51 EDT 2008


On Apr 17, 12:07 pm, Sion Arrowsmith <si... at chiark.greenend.org.uk>
wrote:
>  <s0s... at gmail.com> wrote:
> >In Python, you usually can use parentheses to split something over
> >several lines. But you can't use parentheses for an assignment of
> >several lines.
>
> Yes you can, you just need an iterable of the right length on
> the other side for the tuple unpacking to work:
>
> >>> (CONSTANT1,
>
> ... # This isn't a syntax error
> ...  CONSTANT2,
> ...  CONSTANT3, #and neither is this
> ...  CONSTANT) = [1] * 4>>> [ (k, v) for k, v in locals().items() if k.startswith("CONSTANT") ]
>
> [('CONSTANT', 1), ('CONSTANT1', 1), ('CONSTANT3', 1), ('CONSTANT2', 1)]
>

That's not the same kind of multiple assignment. There, you're just
unpacking several items from a sequence into several variables on the
left. Some would say that's a list assignment.



More information about the Python-list mailing list