Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57241

Newline in bash, was Re: IDLE being too clever checking nonlocal declarations?

From Peter Otten <__peter__@web.de>
Subject Newline in bash, was Re: IDLE being too clever checking nonlocal declarations?
Date 2013-10-22 09:20 +0200
Organization None
References <CAPTjJmpEaQxN10eSU2YzZZ-4wMoT3K=iFBYQqgBx_RZ3AWyfTw@mail.gmail.com> <mailman.1316.1382385139.18130.python-list@python.org> <5265be56$0$29981$c3e8da3$5496439d@news.astraweb.com> <mailman.1332.1382412405.18130.python-list@python.org> <526613ae$0$30000$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.1340.1382426425.18130.python-list@python.org> (permalink)

Show all headers | View raw


Steven D'Aprano wrote:

> On Mon, 21 Oct 2013 23:26:28 -0400, Terry Reedy wrote:
> 
>> On 10/21/2013 7:52 PM, Steven D'Aprano wrote:
>>> On Mon, 21 Oct 2013 15:51:56 -0400, Terry Reedy wrote:
>>>
>>>> On 10/21/2013 11:06 AM, Chris Angelico wrote:
>>>>> Try typing this into IDLE:
>>>>>
>>>>>>>> def a():
>>>>>       def b():
>>>>>           nonlocal q
>>>>> SyntaxError: no binding for nonlocal 'q' found
>>>>
>>>> If you submit those three lines to Python from the command line, that
>>>> is what you see.
>>>
>>> Arguably, that's also too strict,
>> 
>> As I quoted from the doc, it is an error for a program to contain a
>> nonlocal with no referent. The reason is one only needs nonlocal to bind
>> and unlike with 'global newname', it would be undefined where to do the
>> binding.
> 
> Yep, I got that, but what I'm saying is that it is too strict to raise
> the exception at the point where it sees "nonlocal q". The CPython
> interpreter allows q to be defined inside function a but after function
> b, e.g. this is allowed:
> 
> def a():
>     def b():
>         nonlocal q
>         q += 1
>     q = 2  # <=======
> 
> 
> If IDLE and the code.py module requires q to be strictly defined before
> function b, then it is too strict. Your analysis of the bug as being in
> code.py seems plausible.
> 
> 
> 
>>> [steve@ando ~]$ python3.3 -c "def a():
>>>>      def b():
>>>>          nonlocal q
>>>>      q = 1
>>>> "
>> 
>> What system lets you do that? (See other thread about Windows not
>> allowing that, because newline terminates the command even after ".) Is
>> '>' a line continuation marker (like '...' in Python)?
> 
> Yes, sorry I should have said. That's bash, under Linux.
> 
> Here's another way:
> 
> 
> steve@runes:~$ python3.3 -c "def a():^M  def b():^M    nonlocal q^M
> q=1^Mprint(a() is None)"
> True
> 
> 
> Still bash under Linux (a different machine), the ^M is *not* a pair of
> characters ^ followed by M but an actually newline, generated by typing
> Ctrl-V Enter (that's the ENTER key, not the letters E n t e r).
> 
> In theory I should be able to get something working with \n escapes
> instead of ^M, but I can't get it working. But I'm not an expect at bash's
> arcane rules for quoting and escaping special characters.

I usually just hit Return...

$ python3.3 -c "def a():
>   def b():
>     nonlocal q
>   q = 1
> print(a() is None)"
True

but you prompted me to google:

$ python3.3 -c $'def a():\n def b():\n  nonlocal q\n q = 1\nprint(a() is None)'
True

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: IDLE being too clever checking nonlocal declarations? Terry Reedy <tjreedy@udel.edu> - 2013-10-21 15:51 -0400
  Re: IDLE being too clever checking nonlocal declarations? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-21 23:52 +0000
    Re: IDLE being too clever checking nonlocal declarations? Terry Reedy <tjreedy@udel.edu> - 2013-10-21 23:26 -0400
      Re: IDLE being too clever checking nonlocal declarations? Steven D'Aprano <steve@pearwood.info> - 2013-10-22 05:57 +0000
        Re: IDLE being too clever checking nonlocal declarations? Chris Angelico <rosuav@gmail.com> - 2013-10-22 17:24 +1100
        Newline in bash, was Re: IDLE being too clever checking nonlocal declarations? Peter Otten <__peter__@web.de> - 2013-10-22 09:20 +0200

csiph-web