[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)

Chris Angelico rosuav at gmail.com
Wed Jun 27 07:08:28 EDT 2018


On Wed, Jun 27, 2018 at 7:19 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, Jun 27, 2018 at 05:52:16PM +1000, Chris Angelico wrote:
>
>> def test():
>>     a = 1
>>     b = 2
>>     vars = {key: locals()[key] for key in locals()}
>>     return vars
>>
>> What would your intuition say? Should this be equivalent to dict(locals()) ?
>
> That example is so elegant it makes me want to cry.
>
> And not just because you shadowed the vars() builtin *wink*

It gets funnier with nested loops. Or scarier. I've lost the ability
to distinguish those two.

def test():
    spam = 1
    ham = 2
    vars = [key1+key2 for key1 in locals() for key2 in locals()]
    return vars

Wanna guess what that's gonna return?

ChrisA


More information about the Python-Dev mailing list