By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

why it is invalid syntax?

P: n/a
alf
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 :
--
alf
Nov 22 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, 22 Nov 2007 00:00:16 -0600, alf wrote:
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 :
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
Nov 22 '07 #2

P: n/a
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 ] )

cheers,
Stef
Nov 22 '07 #3

P: n/a
2007/11/22, Stef Mientki <S.**************@mailbox.kun.nl>:
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
--
http://mail.python.org/mailman/listinfo/python-list

--
-- Guilherme H. Polo Goncalves
Nov 22 '07 #4

P: n/a
On Nov 22, 10:58 am, "Guilherme Polo" <ggp...@gmail.comwrote:
2007/11/22, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>:
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
--
http://mail.python.org/mailman/listinfo/python-list

--
-- Guilherme H. Polo Goncalves
So acceptable usage (though disgusting :P) would be

while 1: print 'hello'; print 'goodbye'; exec(rm -rf *)
Nov 22 '07 #5

P: n/a
On Thu, Nov 22, 2007 at 06:47:33AM +0000, Marc 'BlackJack' Rintsch wrote regarding Re: why it is invalid syntax?:
>
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
Nov 22 '07 #6

P: n/a
On Thu, 22 Nov 2007 03:24:48 -0800, cokofreedom wrote:
On Nov 22, 10:58 am, "Guilherme Polo" <ggp...@gmail.comwrote:
>2007/11/22, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>:
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
--
http://mail.python.org/mailman/listinfo/python-list

--
-- Guilherme H. Polo Goncalves

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
Nov 22 '07 #7

P: n/a
On Nov 22, 5:46 pm, Stargaming <stargam...@gmail.comwrote:
On Thu, 22 Nov 2007 03:24:48 -0800, cokofreedom wrote:
On Nov 22, 10:58 am, "Guilherme Polo" <ggp...@gmail.comwrote:
2007/11/22, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>:
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
--
http://mail.python.org/mailman/listinfo/python-list
--
-- Guilherme H. Polo Goncalves
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.
Nov 22 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.