[Python-ideas] A different kind of context manager
Kristján Valur Jónsson
kristjan at ccpgames.com
Mon Oct 21 23:05:32 CEST 2013
>And if you changed the rules so everything was nonlocal by default in a context function, we'd need a new keyword to declare local variables, which would be (a) very different from the > rest of python, and (b) hard to come up with a name for that didn't conflict with thousands of different programs.
Well no. You are not defining a function, so all variables are in the same scope as the existing code. Binding does not change. “nonlocal” has exactly the same meaning, meaning outside the function you are writing. The “anonymous callable” exists only technically, not syntactically. It has no local variables, no (visible) arguments.
Semantics stay exactly the same, only the method of invoking the executable code changes.
K
-----Original Message-----
From: Andrew Barnert [mailto:abarnert at yahoo.com]
Sent: 21. október 2013 16:26
To: Kristján Valur Jónsson
Cc: Chris Angelico; python-ideas
Subject: Re: [Python-ideas] A different kind of context manager
A paradigm case for with is creating a new variable in a context to use outside of it:
with open(path) as f:
rows = list(reader(f))
for row in rows[1:]:
with lock:
connections = self.connections
for connection in connections:
The compiler would not make rows or connections into a closure variable, but a local. So you'd have to write nonlocal in most with statements.
And if you changed the rules so everything was nonlocal by default in a context function, we'd need a new keyword to declare local variables, which would be (a) very different from the rest of python, and (b) hard to come up with a name for that didn't conflict with thousands of different programs.
>
> K
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org<mailto:Python-ideas at python.org>
> https://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131021/024406aa/attachment.html>
More information about the Python-ideas
mailing list