why it is invalid syntax?

Discussion in 'Python' started by alf, Nov 22, 2007.

  1. alf

    alf Guest

    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
    alf, Nov 22, 2007
    #1
    1. Advertising

  2. 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
    Marc 'BlackJack' Rintsch, Nov 22, 2007
    #2
    1. Advertising

  3. alf

    Zara Guest

    On Thu, 22 Nov 2007 00:00:16 -0600, alf <4all.nl> 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 :
    >


    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
    Zara, Nov 22, 2007
    #3
  4. alf

    Stef Mientki Guest

    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
    Stef Mientki, Nov 22, 2007
    #4
  5. 2007/11/22, Stef Mientki <>:
    > 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
    Guilherme Polo, Nov 22, 2007
    #5
  6. alf

    Guest

    On Nov 22, 10:58 am, "Guilherme Polo" <> wrote:
    > 2007/11/22, Stef Mientki <>:
    >
    >
    >
    > > 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, 2007
    #6
  7. 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
    J. Clifford Dyer, Nov 22, 2007
    #7
  8. alf

    Stargaming Guest

    On Thu, 22 Nov 2007 03:24:48 -0800, cokofreedom wrote:

    > On Nov 22, 10:58 am, "Guilherme Polo" <> wrote:
    >> 2007/11/22, Stef Mientki <>:
    >>
    >>
    >>
    >> > 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
    Stargaming, Nov 22, 2007
    #8
  9. alf

    Guest

    On Nov 22, 5:46 pm, Stargaming <> wrote:
    > On Thu, 22 Nov 2007 03:24:48 -0800, cokofreedom wrote:
    > > On Nov 22, 10:58 am, "Guilherme Polo" <> wrote:
    > >> 2007/11/22, Stef Mientki <>:

    >
    > >> > 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, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick.O.Ige
    Replies:
    1
    Views:
    1,959
    Patrick.O.Ige
    Jul 2, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    880
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,850
    Smokey Grindel
    Dec 2, 2006
  4. Java Guy
    Replies:
    1
    Views:
    682
    Manish Pandit
    Oct 15, 2006
  5. Nathan Pinno
    Replies:
    9
    Views:
    346
    Mensanator
    Mar 12, 2008
Loading...

Share This Page