syntax for -c cmd

Fredrik Lundh fredrik at pythonware.com
Fri May 12 08:47:05 CEST 2006


Edward Elliott wrote:

> Now this is interesting.  I broke the line up into separate arguments and it
> seemed to work fine:
>
> $ python -c 'if 1==1: print "yes"' 'else: print "no"'
> yes
>
> But then I tested the else branch and it produces no output:
> $ python -c 'if 1==0: print "yes"' 'else: print "no"'
> $
>
> If putting the else in a separate arg unbinds it from the if, I would expect
> a syntax error.  If OTOH naked elses are allowed on the command line for
> some odd reason, then this shouldn't happen:
>
> $ python -c  'else: print "no"'
>   File "<string>", line 1
>     else: print "no"
>        ^
> SyntaxError: invalid syntax
>
> What's with the silent failure in the two-arg version?  If the else arg is
> syntactically acceptable, why doesn't it behave as expected?

hint:

    $ python -c 'import sys; print sys.argv' 'else: print "no"'

</F>






More information about the Python-list mailing list