nonlocal x = value

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

nonlocal x = value

Stefan Behnel-3
Hi,

it seems that Py3 doesn't support setting a "nonlocal" value as part of the
"nonlocal" command

     Python 3.2a4+ (py3k:86480, Nov 16 2010, 16:43:22)
     [GCC 4.4.3] on linux2
     Type "help", "copyright", "credits" or "license" for more information.
     >>> def x():
     ...   y = 5
     ...   def f():
     ...     nonlocal y = 6
       File "<stdin>", line 4
         nonlocal y = 6
                    ^
     SyntaxError: invalid syntax

even though the PEP clearly describes this feature.

http://www.python.org/dev/peps/pep-3104/#proposed-solution

Either the PEP or the implementation should be updated. Personally, I think
the PEP should be changed as I think that the syntax complicates the
grammar more than it's worth. Also, the moratorium applies here, given that
Py3.1 does not implement this.

Comments?

Stefan

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Benjamin Peterson-3
2010/12/17 Stefan Behnel <[hidden email]>:

> Hi,
>
> it seems that Py3 doesn't support setting a "nonlocal" value as part of the
> "nonlocal" command
>
>    Python 3.2a4+ (py3k:86480, Nov 16 2010, 16:43:22)
>    [GCC 4.4.3] on linux2
>    Type "help", "copyright", "credits" or "license" for more information.
>    >>> def x():
>    ...   y = 5
>    ...   def f():
>    ...     nonlocal y = 6
>      File "<stdin>", line 4
>        nonlocal y = 6
>                   ^
>    SyntaxError: invalid syntax
>
> even though the PEP clearly describes this feature.
>
> http://www.python.org/dev/peps/pep-3104/#proposed-solution
>
> Either the PEP or the implementation should be updated. Personally, I think
> the PEP should be changed as I think that the syntax complicates the grammar
> more than it's worth. Also, the moratorium applies here, given that Py3.1
> does not implement this.
>
> Comments?

There's a issue and a patch for this somewhere. I personally don't
care; it's not too painful to write two lines.



--
Regards,
Benjamin
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Laurens Van Houtven
In reply to this post by Stefan Behnel-3
+1 for throwing it out of the PEP. Assignment is a thing,
nonlocal/global is a thing, don't mix them up :) (That in addition to
the grammar cleanliness argument Stephan already made)

cheers
lvh.
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Alexander Belopolsky
On Fri, Dec 17, 2010 at 11:52 AM, Laurens Van Houtven <[hidden email]> wrote:
> +1 for throwing it out of the PEP. Assignment is a thing,
> nonlocal/global is a thing, don't mix them up :) (That in addition to
> the grammar cleanliness argument Stephan already made)

Another +1 for the same reasons.  Also, since global does not allow
assignment, neither should nonlocal.
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Benjamin Peterson-3
2010/12/17 Alexander Belopolsky <[hidden email]>:
> On Fri, Dec 17, 2010 at 11:52 AM, Laurens Van Houtven <[hidden email]> wrote:
>> +1 for throwing it out of the PEP. Assignment is a thing,
>> nonlocal/global is a thing, don't mix them up :) (That in addition to
>> the grammar cleanliness argument Stephan already made)
>
> Another +1 for the same reasons.  Also, since global does not allow
> assignment, neither should nonlocal.

Note that the PEP stated that global would also be extended.



--
Regards,
Benjamin
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Alexander Belopolsky
On Fri, Dec 17, 2010 at 12:33 PM, Benjamin Peterson <[hidden email]> wrote:
..
>> Another +1 for the same reasons.  Also, since global does not allow
>> assignment, neither should nonlocal.
>
> Note that the PEP stated that global would also be extended.

I missed that, so for future reference, the PEP says:

"""
A shorthand form is also permitted, in which nonlocal is prepended to
an assignment or augmented assignment:

nonlocal x = 3
The above has exactly the same meaning as nonlocal x; x = 3. (Guido
supports a similar form of the global statement.)
"""

and refers to Guido's post at

http://mail.python.org/pipermail/python-3000/2006-November/004166.html

In any case, the relevant issue is

http://bugs.python.org/issue4199

and it should probably be marked as "after moratorium".  Meanwhile an
implementation status note can be added to the PEP to avoid this issue
being brought up again.
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Georg Brandl-2
In reply to this post by Laurens Van Houtven
Am 17.12.2010 17:52, schrieb Laurens Van Houtven:
> +1 for throwing it out of the PEP. Assignment is a thing,
> nonlocal/global is a thing, don't mix them up :) (That in addition to
> the grammar cleanliness argument Stephan already made)

The trouble is what to make of

nonlocal x = 3, y

Is it two nonlocal declarations or one with a tuple assignment?

Georg

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Laurens Van Houtven
On Sat, Dec 18, 2010 at 1:12 PM, Georg Brandl <[hidden email]> wrote:

> Am 17.12.2010 17:52, schrieb Laurens Van Houtven:
>> +1 for throwing it out of the PEP. Assignment is a thing,
>> nonlocal/global is a thing, don't mix them up :) (That in addition to
>> the grammar cleanliness argument Stephan already made)
>
> The trouble is what to make of
>
> nonlocal x = 3, y
>
> Is it two nonlocal declarations or one with a tuple assignment?
>
> Georg

I'm not sure I understand. Isn't that another reason to throw it out?
If you don't allow such assignments, there can't be any ambiguity,
right? (Or am I missing something?)

lvh
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Terry Reedy
On 12/22/2010 5:11 PM, Laurens Van Houtven wrote:

> On Sat, Dec 18, 2010 at 1:12 PM, Georg Brandl<[hidden email]>  wrote:
>> Am 17.12.2010 17:52, schrieb Laurens Van Houtven:
>>> +1 for throwing it out of the PEP. Assignment is a thing,
>>> nonlocal/global is a thing, don't mix them up :) (That in addition to
>>> the grammar cleanliness argument Stephan already made)
>>
>> The trouble is what to make of
>>
>> nonlocal x = 3, y
>>
>> Is it two nonlocal declarations or one with a tuple assignment?
>>
>> Georg
>
> I'm not sure I understand. Isn't that another reason to throw it out?

I am sure he meant it to be, and I agree. We lived without
global x = 3, y
all these years.

> If you don't allow such assignments, there can't be any ambiguity,
> right? (Or am I missing something?)


--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Georg Brandl-2
In reply to this post by Laurens Van Houtven
Am 22.12.2010 23:11, schrieb Laurens Van Houtven:

> On Sat, Dec 18, 2010 at 1:12 PM, Georg Brandl <[hidden email]> wrote:
>> Am 17.12.2010 17:52, schrieb Laurens Van Houtven:
>>> +1 for throwing it out of the PEP. Assignment is a thing,
>>> nonlocal/global is a thing, don't mix them up :) (That in addition to
>>> the grammar cleanliness argument Stephan already made)
>>
>> The trouble is what to make of
>>
>> nonlocal x = 3, y
>>
>> Is it two nonlocal declarations or one with a tuple assignment?
>>
>> Georg
>
> I'm not sure I understand. Isn't that another reason to throw it out?
> If you don't allow such assignments, there can't be any ambiguity,
> right? (Or am I missing something?)

Yes and no -- there may not be an ambiguity to the parser, but still to
the human.  Except if you disallow the syntax in any case, requiring
people to write

nonlocal x = (3, y)

which is then again inconsistent with ordinary assignment statements.

Georg

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Laurens Van Houtven
On Thu, Dec 23, 2010 at 9:51 PM, Georg Brandl <[hidden email]> wrote:
> Yes and no -- there may not be an ambiguity to the parser, but still to
> the human.  Except if you disallow the syntax in any case, requiring
> people to write
>
> nonlocal x = (3, y)
>
> which is then again inconsistent with ordinary assignment statements.
>
> Georg

Right -- but (and hence the confusion) I was arguing for not mixing
global/nonlocal with assignment at all, and instead having nonlocal
and global only take one or more names. That would (obviously) remove
any such ambiguity ;-)

cheers
lvh
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Georg Brandl-2
Am 23.12.2010 22:03, schrieb Laurens Van Houtven:

> On Thu, Dec 23, 2010 at 9:51 PM, Georg Brandl <[hidden email]> wrote:
>> Yes and no -- there may not be an ambiguity to the parser, but still to
>> the human.  Except if you disallow the syntax in any case, requiring
>> people to write
>>
>> nonlocal x = (3, y)
>>
>> which is then again inconsistent with ordinary assignment statements.
>>
>> Georg
>
> Right -- but (and hence the confusion) I was arguing for not mixing
> global/nonlocal with assignment at all, and instead having nonlocal
> and global only take one or more names. That would (obviously) remove
> any such ambiguity ;-)

Oh yes, I see -- not sure why I worded it like I did.  I just wanted to
reiterate the most obvious problematic point to people who hadn't followed
the earlier discussions about it.  Sorry.

Georg

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Hrvoje Niksic-2
In reply to this post by Laurens Van Houtven
On 12/23/2010 10:03 PM, Laurens Van Houtven wrote:

> On Thu, Dec 23, 2010 at 9:51 PM, Georg Brandl<[hidden email]>  wrote:
>>  Yes and no -- there may not be an ambiguity to the parser, but still to
>>  the human.  Except if you disallow the syntax in any case, requiring
>>  people to write
>>
>>  nonlocal x = (3, y)
>>
>>  which is then again inconsistent with ordinary assignment statements.
>
> Right -- but (and hence the confusion) I was arguing for not mixing
> global/nonlocal with assignment at all, and instead having nonlocal
> and global only take one or more names. That would (obviously) remove
> any such ambiguity ;-)

I would like to offer the opposing viewpoint: nonlocal x = value is a
useful shortcut because nonlocal is used in closure callbacks where
brevity matters.  The reason nonlocal is introduced is to change the
variable, so it makes sense that the two can be done in the same line of
code.

As for global x = value being disallowed, I have been annoyed at times
with that, so that sounds like a good argument to change both.

Requiring the parentheses for tuple creation sounds like a good
compromise for resolving the ambiguity, consistent with similar
limitations of the generator expression syntax.
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Stefan Behnel-3
Hrvoje Niksic, 24.12.2010 09:45:

> On 12/23/2010 10:03 PM, Laurens Van Houtven wrote:
>> On Thu, Dec 23, 2010 at 9:51 PM, Georg Brandl wrote:
>>> Yes and no -- there may not be an ambiguity to the parser, but still to
>>> the human. Except if you disallow the syntax in any case, requiring
>>> people to write
>>>
>>> nonlocal x = (3, y)
>>>
>>> which is then again inconsistent with ordinary assignment statements.
>>
>> Right -- but (and hence the confusion) I was arguing for not mixing
>> global/nonlocal with assignment at all, and instead having nonlocal
>> and global only take one or more names. That would (obviously) remove
>> any such ambiguity ;-)
>
> I would like to offer the opposing viewpoint: nonlocal x = value is a
> useful shortcut because nonlocal is used in closure callbacks where
> brevity matters.

I doubt that it really matters so much that one line more kills
readability. It's still a relatively rare use case after all.


> The reason nonlocal is introduced is to change the
> variable, so it makes sense that the two can be done in the same line of
> code.

But still, this is just a special case. If the variable is changed more
than once, you'd end up with one assignment with nonlocal and one without.
This just adds to the growing list of code inconsistencies presented in
this thread.


> As for global x = value being disallowed, I have been annoyed at times with
> that, so that sounds like a good argument to change both.
>
> Requiring the parentheses for tuple creation sounds like a good compromise
> for resolving the ambiguity, consistent with similar limitations of the
> generator expression syntax.

Why introduce such a pitfall for coders here? "Require" doesn't mean the
parser can enforce it. If it's not there, it just means something else, so
it's up to the coder to get it right. The exact same kind of situation that
was fixed for the except syntax in Python 3.

Stefan

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Raymond Hettinger-4

On Dec 25, 2010, at 2:59 AM, Stefan Behnel wrote:

> Hrvoje Niksic, 24.12.2010 09:45:
>> On 12/23/2010 10:03 PM, Laurens Van Houtven wrote:
>>> On Thu, Dec 23, 2010 at 9:51 PM, Georg Brandl wrote:
>>>> Yes and no -- there may not be an ambiguity to the parser, but still to
>>>> the human. Except if you disallow the syntax in any case, requiring
>>>> people to write
>>>>
>>>> nonlocal x = (3, y)
>>>>
>>>> which is then again inconsistent with ordinary assignment statements.
>>>
>>> Right -- but (and hence the confusion) I was arguing for not mixing
>>> global/nonlocal with assignment at all, and instead having nonlocal
>>> and global only take one or more names. That would (obviously) remove
>>> any such ambiguity ;-)
>>
>> I would like to offer the opposing viewpoint: nonlocal x = value is a
>> useful shortcut because nonlocal is used in closure callbacks where
>> brevity matters.
>
> I doubt that it really matters so much that one line more kills readability. It's still a relatively rare use case after all.
>
>
>> The reason nonlocal is introduced is to change the
>> variable, so it makes sense that the two can be done in the same line of
>> code.

FWIW, I'm entirely opposed to doing an assignment in a nonlocal definition.

* It is easily mis-parsed by human (as shown by Georg's examples).
* It looks very much like an initialization of a local variable in many languages,
  but it is not -- the variable has already been initialized in another scope.
* It is not clear how to extend it to multiple variables (which we already allow).
* It is entirely unnecessary.  Just add a real assignment on the following line:
         local x
         x = 3, y
* We've had global declarations for a very long time and never needed
   (or wanted) an assignment for it:
        global x = 3, y
* The purported use case is rare (at best).  Special cases aren't worth breaking the rules.
   And the purported goal (saving one line) isn't much of a payoff.
* The language moratorium is ending but the aversion to non-essential
   micro-syntax changes persists.
* And, Georg doesn't like it :-)


Raymond
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Mark Dickinson
On Mon, Dec 27, 2010 at 9:43 AM, Raymond Hettinger
<[hidden email]> wrote:
> FWIW, I'm entirely opposed to doing an assignment in a nonlocal definition.
> [...]

-1 for assignment in nonlocal and global statements from me, too.

Mark
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Nick Coghlan
On Mon, Dec 27, 2010 at 8:31 PM, Mark Dickinson <[hidden email]> wrote:
> On Mon, Dec 27, 2010 at 9:43 AM, Raymond Hettinger
> <[hidden email]> wrote:
>> FWIW, I'm entirely opposed to doing an assignment in a nonlocal definition.
>> [...]
>
> -1 for assignment in nonlocal and global statements from me, too.

Indeed. The PEP should be updated to be clear that that part was never
implemented (referencing Raymond's post for the reasons why).

Cheers,
Nick.

--
Nick Coghlan   |   [hidden email]   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: nonlocal x = value

Georg Brandl-2
In reply to this post by Raymond Hettinger-4
Am 27.12.2010 10:43, schrieb Raymond Hettinger:

> FWIW, I'm entirely opposed to doing an assignment in a nonlocal definition.
>
> * It is easily mis-parsed by human (as shown by Georg's examples).
> * It looks very much like an initialization of a local variable in many languages,
>   but it is not -- the variable has already been initialized in another scope.

Hah, and we'd probably get requests for a mandatory declaration of locals, ala

local a = 5

for reasons of "consistency" or "explicit is better than implicit".

Georg


_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%40nabble.com