[Python-ideas] PEP 572: Statement-Local Name Bindings, take three!

Paul Moore p.f.moore at gmail.com
Fri Mar 23 06:38:19 EDT 2018


On 23 March 2018 at 10:01, Chris Angelico <rosuav at gmail.com> wrote:
>     # ... except when function bodies are involved...
>     if (input("> ") as cmd):
>         def run_cmd():
>             print("Running command", cmd) # NameError
>
>     # ... but function *headers* are executed immediately
>     if (input("> ") as cmd):
>         def run_cmd(cmd=cmd): # Capture the value in the default arg
>             print("Running command", cmd) # Works

What about

    cmd = "Something else"
    if (input("> ") as cmd):
        def run_cmd():
            print("Running command", cmd) # Closes over the "outer"
cmd, not the statement-local one?

Did I get that right? I don't really like it if so (I think it's
confusing) but I guess I could live with "well, don't do that then" as
an answer. And I don't have a better interpretation.

I'm still not convinced I like the proposal, but it's a lot cleaner
than previous versions, so thanks for that. Far fewer places where I
said "hmm, I don't understand the implications".

Paul


More information about the Python-ideas mailing list