why it is invalid syntax?

A

alf

Hi,

I wonder why it is an invalid syntax:

File "<stdin>", line 1
if 1: if 1: if 1: print 1


or
File "<stdin>", line 1
if 1: for i in range(10): print i

I would expect one could nest :
 
M

Marc 'BlackJack' Rintsch

I wonder why it is an invalid syntax:


File "<stdin>", line 1
if 1: if 1: if 1: print 1

or

File "<stdin>", line 1
if 1: for i in range(10): print i

I would expect one could nest :

It's quite unreadable and if this would be allowed you would have to
introduce a special rule to forbid ``else``, ``except`` and ``finally``
because it can lead to ambiguities. To which ``if`` does the ``else``
belong to here? ::

if 1: print 1 if: 1 print 1 else: print 1

Ciao,
Marc 'BlackJack' Rintsch
 
Z

Zara

Hi,

I wonder why it is an invalid syntax:


File "<stdin>", line 1
if 1: if 1: if 1: print 1


or

File "<stdin>", line 1
if 1: for i in range(10): print i

I would expect one could nest :

You may take a look at python documentation, Language reference, 7.
Compound statement. It clearly states that what follows the : of
if,while,for... if it is not a new line, it should be a list of simple
statements. Son no there is no : nesting.

compound_stmt ::= if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]

Regards,

Zara
 
S

Stef Mientki

alf said:
Hi,

I wonder why it is an invalid syntax:


File "<stdin>", line 1
if 1: if 1: if 1: print 1


or

File "<stdin>", line 1
if 1: for i in range(10): print i

I would expect one could nest :

Although I agree it might be quit unreadable for normal programmers,
people who are used to writing math formula, (i.e. MatLab),
this is not true.

Here another interesting one, that is accepted:

self.nodes.extend ( [ ONode(shape,n,self) \
for n in range(shape.Parent.N_Outputs) \
if shape.Type_Outputs[n] == type ] )

cheers,
Stef
 
G

Guilherme Polo

2007/11/22 said:
alf said:
Hi,

I wonder why it is an invalid syntax:


File "<stdin>", line 1
if 1: if 1: if 1: print 1


or

File "<stdin>", line 1
if 1: for i in range(10): print i

I would expect one could nest :

Although I agree it might be quit unreadable for normal programmers,
people who are used to writing math formula, (i.e. MatLab),
this is not true.

Here another interesting one, that is accepted:

self.nodes.extend ( [ ONode(shape,n,self) \
for n in range(shape.Parent.N_Outputs) \
if shape.Type_Outputs[n] == type ] )

That is a list comprehension
 
C

cokofreedom

2007/11/22, Stef Mientki <[email protected]>:


Although I agree it might be quit unreadable for normal programmers,
people who are used to writing math formula, (i.e. MatLab),
this is not true.
Here another interesting one, that is accepted:
self.nodes.extend ( [ ONode(shape,n,self) \
for n in range(shape.Parent.N_Outputs) \
if shape.Type_Outputs[n] == type ] )

That is a list comprehension


cheers,
Stef

So acceptable usage (though disgusting :p) would be

while 1: print 'hello'; print 'goodbye'; exec(rm -rf *)
 
J

J. Clifford Dyer

It's quite unreadable and if this would be allowed you would have to
introduce a special rule to forbid ``else``, ``except`` and ``finally``
because it can lead to ambiguities. To which ``if`` does the ``else``
belong to here? ::

if 1: print 1 if: 1 print 1 else: print 1

Ciao,
Marc 'BlackJack' Rintsch

I don't reckon in matters much. Your output will be:

1
1

;)

No, actually on second inspection your output will be:

File "<stdin>", line 1
if 1: print 1 if: 1 print 1 else: print 1
^
SyntaxError: invalid syntax

But it's a good point.

Cheers,
Cliff
 
S

Stargaming

2007/11/22, Stef Mientki <[email protected]>:


alf wrote:
Hi,
I wonder why it is an invalid syntax:
if 1: if 1: if 1: print 1
File "<stdin>", line 1
if 1: if 1: if 1: print 1

if 1: for i in range(10): print i
File "<stdin>", line 1
if 1: for i in range(10): print i
I would expect one could nest :
Although I agree it might be quit unreadable for normal programmers,
people who are used to writing math formula, (i.e. MatLab), this is
not true.
Here another interesting one, that is accepted:
self.nodes.extend ( [ ONode(shape,n,self) \
for n in range(shape.Parent.N_Outputs)
\ if shape.Type_Outputs[n] == type ] )

That is a list comprehension


cheers,
Stef

So acceptable usage (though disgusting :p) would be

while 1: print 'hello'; print 'goodbye'; exec(rm -rf *)

Nope::

exec(rm -rf *)
^
SyntaxError: invalid syntax

Even the syntactically correct ``exec("rm -rf *")`` would make your
computer explode. Should we introduce this as a shortcut to `break`? ;-)

SCNR,
stargaming
 
C

cokofreedom

2007/11/22, Stef Mientki <[email protected]>:
alf wrote:
Hi,
I wonder why it is an invalid syntax:
if 1: if 1: if 1: print 1
File "<stdin>", line 1
if 1: if 1: if 1: print 1
or
if 1: for i in range(10): print i
File "<stdin>", line 1
if 1: for i in range(10): print i
I would expect one could nest :
Although I agree it might be quit unreadable for normal programmers,
people who are used to writing math formula, (i.e. MatLab), this is
not true.
Here another interesting one, that is accepted:
self.nodes.extend ( [ ONode(shape,n,self) \
for n in range(shape.Parent.N_Outputs)
\ if shape.Type_Outputs[n] == type ] )
That is a list comprehension
cheers,
Stef
So acceptable usage (though disgusting :p) would be
while 1: print 'hello'; print 'goodbye'; exec(rm -rf *)

Nope::

exec(rm -rf *)
^
SyntaxError: invalid syntax

Even the syntactically correct ``exec("rm -rf *")`` would make your
computer explode. Should we introduce this as a shortcut to `break`? ;-)

SCNR,
stargaming

Haha, you are correct. I was tempted to actually trial and error the
code too much...

I feel it is an important thing to present to a new user however, much
like the infinite "alert message" because of their infinite loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top