[Tutor] Please Help

Mats Wichmann mats at wichmann.us
Sat Sep 7 17:32:59 EDT 2019


On 9/7/19 3:03 PM, Eryk Sun wrote:
> 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')

yeah, and I'm looking forward to using this... once I get myself off of
projects that try to carry compat for a whole bunch of older Python
versions, that is :)




More information about the Tutor mailing list