I remain -1 on the PEP, but thank you very much Chris for the great work
writing it and the extra clarifications in it.

The part I like most about the proposal is the use in blocks, like:

if (re.match(...) as m):
    print(m.groups(0))

if (input("> ") as cmd):
    def run_cmd(cmd=cmd): # Capture the value in the default arg
        print("Running command", cmd) # Works


But what I like has nothing much to do with scope limitation.  It is only
about the idea of introducing a variable into a block.  We can do that with
a context manager.  No, there's no cleanup of the namespace in a
straightforward approach, but using e.g. `_x` as a convention for names
meant to be transient is already established practice and requires no
special syntax.

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.

I suppose I could write something much more obscure that poked into the
call stack and actually deleted names from scopes in the context manager.
But really I rarely care about the temporary use of a few names, especially
if the same few temporary names are used repeatedly for these things.

On Fri, Mar 2, 2018 at 8:04 AM, Chris Angelico <ros...@gmail.com> wrote:

> On Sat, Mar 3, 2018 at 1:53 AM, Rhodri James <rho...@kynesim.co.uk> wrote:
> > On 02/03/18 11:43, Chris Angelico wrote:
> >>
> >> After dozens of posts and a wide variety of useful opinions and
> >> concerns being raised, here is the newest version of PEP 572 for your
> >> debating pleasure.
> >
> >
> > I haven't said this yet, so thanks Chris for putting this all together.
> Even
> > if the result is a rejected PEP, at least we have everything in one
> place.
>
> No problem. And I agree, a rejected PEP is still a successful result here.
>
> (Am I going to get a reputation for captaining dead PEPs?)
>
> >>      # Compound statements usually enclose everything...
> >>      if (re.match(...) as m):
> >>          print(m.groups(0))
> >>      print(m) # NameError
> >
> >
> > This (and the equivalent in while loops) is the big win in the PEP, in my
> > opinion.  The number of ugly loops I've had to write in Python because I
> > can't write "while (something_to_do() as event):"...  +1 on this.
> >
> >>      # Using a statement-local name
> >>      stuff = [[(f(x) as y), x/y] for x in range(5)]
> >
> >
> > As Paul said, the asymmetry of this bothers me a lot.  It doesn't read
> > naturally to me. -1 on this.
>
> Interesting. I fully expected to get a lot more backlash for the
> if/while usage, but a number of people are saying that that's the only
> (or the biggest) part of this proposal that they like.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to