So I understand that:
The end of a logical line is represented by the token NEWLINE
This means the way Python's grammar is defined the only way to end a logical line is with a \n
token.
The same goes for physical lines (rather an EOL, which is the EOL of the platform you're using when writing the file but nevertheless converted to a universal \n
by Python.
A logical line can or cannot be equivalent to one or more physical lines, but usually it's one, and most of the times it's one if you write clean code.
In the sense that:
foo = 'some_value' # 1 logical line = 1 physical
foo, bar, baz = 'their', 'corresponding', 'values' # 1 logical line = 1 physical
some_var, another_var = 10, 10; print(some_var, another_var); some_fn_call()
# the above is still still 1 logical line = 1 physical line
# because ; is not a terminator per se but a delimiter
# since Python doesn't use EBNF exactly but rather a modified form of BNF
# p.s one should never write code as the last line, it's just for educational purposes
Without showing examples of how 1 logical is equivalent to > 1 physical, my question is the following part from the docs:
Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements)
But what does this even mean? I understand the list of the compound statements, be them: if, while, for, etc. they are all made up of one or multiple clauses and each clause, in turn is made up of a header and a suite. The suite is made up of one or multiple statements, let's take an example to be more specific:
So the if statement is something like this according to the grammar (excluding the elifs and else clauses):
if_stmt ::= "if" expression ":" suite
where the suite and its subsequent statements:
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
so this means that if you want you can choose (given by "|") your suite to be 1 of 2 ways:
on the same line:
disadvantages: not pythonic and you cannot have another compound statement that introduces a new block (like a func def, another if, etc)
advatanges: one liner I guess
example:
if 'truthy_string': foo, bar, baz = 1, 2, 3; print('whatever'); call_some_fn();
introduce a new block:
advantages: all, and the proper way to do it
example:
if 'truthy_value':
first_stmt = 5
second_stmt = 10
a, b, c = 1, 2, 3
func_call()
result = inception(nested(calls(one_param), another_param), yet_another))
but I don't see how
Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax
What I see above is a suite, which is a block of code controlled by the if clause, and in turn, that suite, is made up of logical, independent lines (statements), where each logical line is one physical line (coincidentally). I don't see how one logical line can cross the boundaries (which basically is just a fancy word for the end, the limit, which is newline), I don't see how one statement can cross those boundaries and span into the next statement, or maybe I'm really confused and have everything mixed up, but if someone can please explain.
Thank you for your time in advance.