[Python-ideas] PEP 572 version 2: Statement-Local Name Bindings

Ethan Furman ethan at stoneleaf.us
Fri Mar 2 13:44:52 EST 2018


On 03/02/2018 09:34 AM, David Mertz wrote:

> So right now, I can do these:
>
>     class bind(object):
>          def __init__(self, *args):
>              self.args = args
>          def __enter__(self):
>              return self.args[0] if len(self.args)==1 else self.args
>          def __exit__(self, *args):
>              pass
>
>      >>> with bind(sqrt(2)) as _a:
>     ...     print(_a)
>     1.4142135623730951
>
>      >>> with bind(sqrt(2), log(2)) as (a, b):
>     ...     print(a, b, a+b)
>     1.4142135623730951 0.6931471805599453 2.1073607429330403
>
>
> This would cover 98% of the cases that I would want with the proposed statement-local name bindings.

Cool!  But what's the advantage over simple assignment?

--
~Ethan~


More information about the Python-ideas mailing list