[Python-ideas] A "local" pseudo-function
Tim Delaney
timothy.c.delaney at gmail.com
Sun Apr 29 01:15:23 EDT 2018
On Sun, 29 Apr 2018 at 10:30, Tim Peters <tim.peters at gmail.com> wrote:
> [Tim Delaney <timothy.c.delaney at gmail.com>]
> > My big concern here involves the:
> >
>
> > if local(m = re.match(regexp, line)):
> > print(m.group(0))
> >
> > example. The entire block needs to be implicitly local for that to work -
> > what happens if I assign a new name in that block?
>
> I really don't know what you're asking there. Can you make it
> concrete? If, e.g., you're asking what happens if this appeared after
> the `print`:
>
> x = 3.14
>
> then the answer is "the same as what would happen if `local` had not
> been used". We can't know what that is without context, though.
> Maybe x is global. Maybe x was declared nonlocal earlier. Maybe it's
> function-local. While it may be irrelevant to what you're asking, I
> noted just before:
>
That's exactly what I was asking, and as I understand what you're saying,
we would have a local name m available in the indented block which went
away when the block ended, but any names modified in the block are not
local to the block. That seems likely to be a source of errors.
To clarify my understanding, if the names 'x' and 'm' did not exist prior
to the following code, what would x and m refer to after the block
completed?
if local(m = re.match(regexp, line)):
x = 1
m = 2
> >> if local(m = re.match(regexp, line)):
> >> print(m.group(0))
>
> > if local { m = re.match(regexp, line) }:
> > print(m.group(0))
>
> OK, this is the only case in which you used it in an `if` or `while`
> expression. All the questions you asked of me at the start can be
> asked of this spelling too.
>
> You seemed to imply at the start that the
> right curly brace would always mark the end of the new scope. But if
> that's so, the `m` in `m.group()` has nothing to do with the `m`
> assigned to in the `local` block - _that_ scope ended before `print`
> was reached.
>
Yes - I think this is exactly the same issue as with your proposed syntax.
> So if you're not just trying to increase the level of complexity of
> what can appear in a local block, a fundamental problem still needs
> solving ;-) I suppose you could solve it like so:
>
> local { m = re.match(regexp, line)
> if m:
> print(m.group(0))
> }
>
> but, besides losing the "shortcut", it would also mean something
> radically different if
>
> x = 3.14
>
> appeared after the "print". Right? If a "local block" is taken
> seriously, then _all_ names bound inside it vanish when the block
> ends.
Indeed, and I don't have a proposal - just concerns it wold be very
difficult to explain and understand exactly what would happen in the case
of something like:
if local(m = re.match(regexp, line)):
x = 1
m = 2
Regarding the syntax, I didn't want to really change your proposal, but
just thought the functionality was different enough from the function call
it appears to be that it probably merits different syntax.
Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180429/72f980a7/attachment.html>
More information about the Python-ideas
mailing list