On Sat, Mar 3, 2018 at 5:30 AM, Brendan Barnwell <brenb...@brenbarn.net> wrote:
>         But if that's the case, maybe what we want is actually another thing
> that's been discussed many times on this list, namely something like a
> with-block that can define "super-local" variables that disappear at the end
> of the block:
> with b**2 - 4*a*c as D:
>     x = [(-b + sqrt(D))/(2*a), (-b - sqrt(D))/(2*a)]
>         This the same as my earlier version with "with", except the with
> clause comes at the beginning.
>         There's no need to go into extreme detail here on these proposals as
> they're not really what's proposed by this PEP.

Maybe they should be. There are two halves to this proposal:

1) A syntax for capturing an expression with a name binding
2) A scoping system for statement-local names.

I'm already seeing some support for the first half without the second
("just make it a regular assignment"). Do we need two completely
separate PEPs to handle these two proposals? They could link to each
other saying "this could work well with that", but they have
independent value. Statement-local names without a syntax for name
binding would be useful in basically two contexts - the "with" syntax
given here, and "except Exception as e:", which currently uses a bit
of a weird hack of unbinding the existing name.

But the 'with' statement would then have a messy dual form. It can be
used for name bindings, and it can also be used for resource
management. The easiest way to handle this is:

def object.__enter__(self): return self
def object.__exit__(self, *a): pass

But I don't know that people want that.

Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to