[Tutor] Please Help

Eryk Sun eryksun at gmail.com
Sat Sep 7 17:03:44 EDT 2019


On 9/7/19, Mats Wichmann <mats at wichmann.us> wrote:
> On 9/7/19 6:22 AM, Sean Frazier wrote:
>>
>> > [var = open('file.txt')]
>
> what you have written is invalid - you are trying to do an assignment
> inside a list specifier, Python's syntax doesn't allow that.

True, since the above is using the regular assignment operator (=) for
an assignment statement. However, starting with 3.8, it's no longer
the case that all assignment is disallowed in expressions. We now have
the walrus operator (:=) for assignment *expressions*. Thus, now for
something completely different:

    >>> files = [spam := open('spam.txt'), eggs := open('eggs.txt')]
    >>> spam.name, eggs.name
    ('spam.txt', 'eggs.txt')


More information about the Tutor mailing list