... def g(): passdef f(): pass
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 g(): passdef f(): pass
File "<stdin>", line 2
def g(): pass
^
SyntaxError: invalid syntax
Is there a good reason for this?
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 g(): passdef f(): pass
File "<stdin>", line 2
def g(): pass
^
SyntaxError: invalid syntax
Is there a good reason for this?
This 2-line programThe 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
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?
On 26/06/2022 23:22, Jon Ribbens via Python-list wrote:
On 2022-06-26, Rob Cliffe <rob.cliffe@btinternet.com> wrote:It's actually not to do with 1-line blocks, just attempting to define 2 functions "at once":
This 2-line programFor some reason, the REPL can't cope with one-line blocks like that.
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?
If you put a blank line after each one-block line then it will work.
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.
... return 42def f():
... 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".
On 2022-06-26, Rob Cliffe <rob.cliffe@btinternet.com> wrote:It's actually not to do with 1-line blocks, just attempting to define 2 functions "at once":
This 2-line programFor some reason, the REPL can't cope with one-line blocks like that.
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 g(): passdef f(): pass
File "<stdin>", line 2
def g(): pass
^
SyntaxError: invalid syntax
Is there a good reason for this?
If you put a blank line after each one-block line then it will work.
... return 42def f():
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 241:15:11 |
Calls: | 6,624 |
Files: | 12,173 |
Messages: | 5,320,138 |