• REPL with multiple function definitions

    From Rob Cliffe@21:1/5 to All on Sun Jun 26 23:14:13 2022
    This 2-line program

    def f(): pass
    def g(): pass

    runs silently (no Exception).  But:

    23:07:02 c:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    def f(): pass
    ... def g(): pass
      File "<stdin>", line 2
        def g(): pass
        ^
    SyntaxError: invalid syntax


    Is there a good reason for this?
    Thanks
    Rob Cliffe

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to python-list@python.org on Mon Jun 27 08:37:00 2022
    On Mon, 27 Jun 2022 at 08:15, Rob Cliffe via Python-list <python-list@python.org> wrote:

    This 2-line program

    def f(): pass
    def g(): pass

    runs silently (no Exception). But:

    23:07:02 c:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    def f(): pass
    ... def g(): pass
    File "<stdin>", line 2
    def g(): pass
    ^
    SyntaxError: invalid syntax


    Is there a good reason for this?

    The REPL compiles one statement at a time. A file is allowed to
    contain multiple statements.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Ribbens@21:1/5 to Rob Cliffe on Sun Jun 26 22:22:32 2022
    On 2022-06-26, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
    This 2-line program

    def f(): pass
    def g(): pass

    runs silently (no Exception).  But:

    23:07:02 c:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    def f(): pass
    ... def g(): pass
      File "<stdin>", line 2
        def g(): pass
        ^
    SyntaxError: invalid syntax


    Is there a good reason for this?

    For some reason, the REPL can't cope with one-line blocks like that.
    If you put a blank line after each one-block line then it will work.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to Rob Cliffe via Python-list on Mon Jun 27 00:37:55 2022
    Rob Cliffe via Python-list schreef op 27/06/2022 om 0:14:
    This 2-line program

    def f(): pass
    def g(): pass

    runs silently (no Exception).  But:

    23:07:02 c:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def f(): pass
    ... def g(): pass
      File "<stdin>", line 2
        def g(): pass
        ^
    SyntaxError: invalid syntax
    >>>

    Is there a good reason for this?
    The REPL requires an extra empty line to indicate the end of multi-line constructs. You can see it by the prompt: as long as the REPL prints
    '... '  as prompt, that means it puts everything you type in the same multi-line construct. To enter a new multi-line construct (such as a
    function definition, a for-loop, an if-statement, ...), press enter
    directly at the prompt; the REPL should than use '>>> ' as the prompt again.

    (Alternatives like IPython (https://ipython.readthedocs.io/en/stable/)
    are a bit more loose regarding how to enter multi-line constructs)

    --
    "Iceland is the place you go to remind yourself that planet Earth is a machine... and that all organic life that has ever existed amounts to a greasy film that has survived on the exterior of that machine thanks to furious improvisation."
    -- Sam Hughes, Ra

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to python-list@python.org on Wed Jun 29 11:13:29 2022
    On Wed, 29 Jun 2022 at 11:00, Rob Cliffe via Python-list <python-list@python.org> wrote:

    On 26/06/2022 23:22, Jon Ribbens via Python-list wrote:
    On 2022-06-26, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
    This 2-line program

    def f(): pass
    def g(): pass

    runs silently (no Exception). But:

    23:07:02 c:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 >> bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information. >>>>> def f(): pass
    ... def g(): pass
    File "<stdin>", line 2
    def g(): pass
    ^
    SyntaxError: invalid syntax
    Is there a good reason for this?
    For some reason, the REPL can't cope with one-line blocks like that.
    If you put a blank line after each one-block line then it will work.
    It's actually not to do with 1-line blocks, just attempting to define 2 functions "at once":


    22:27:23 C:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    def f():
    ... return 42
    ... def g():
    File "<stdin>", line 3
    def g():
    ^
    SyntaxError: invalid syntax


    But you are right that adding a blank line after the first function definition solves the "problem".

    And if you have something where you want to copy and paste multiple
    statements, there are a few ways to do it:

    1) Put "if 1:" at the top. That makes it a single block, so you can
    paste in as much as you like, as long as the only blank line is at the
    end.

    2) Put the code into a file and then use "python3 -i setup.py". That
    runs all the code, then drops you into the REPL in that context.

    3) Put the code into a file, and inside the REPL, "from setup import
    *". Unlike option 2, this can be done after the beginning of the
    session. Downside: editing setup.py and reimporting won't apply your
    changes.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rob Cliffe@21:1/5 to Jon Ribbens via Python-list on Tue Jun 28 22:31:34 2022
    On 26/06/2022 23:22, Jon Ribbens via Python-list wrote:
    On 2022-06-26, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
    This 2-line program

    def f(): pass
    def g(): pass

    runs silently (no Exception).  But:

    23:07:02 c:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    def f(): pass
    ... def g(): pass
      File "<stdin>", line 2
        def g(): pass
        ^
    SyntaxError: invalid syntax
    Is there a good reason for this?
    For some reason, the REPL can't cope with one-line blocks like that.
    If you put a blank line after each one-block line then it will work.
    It's actually not to do with 1-line blocks, just attempting to define 2 functions "at once":


    22:27:23 C:\>python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    def f():
    ...     return 42
    ... def g():
      File "<stdin>", line 3
        def g():
        ^
    SyntaxError: invalid syntax


    But you are right that adding a blank line after the first function
    definition solves the "problem".
    Rob Cliffe

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)