syntax for -c cmd

Edward Elliott nobody at 127.0.0.1
Thu May 11 16:12:55 EDT 2006


James wrote:
> Wrong syntax is shown below. What should be the delimiter before else?
> 
> python -c 'if 1==1: print "yes"; else print "no"'

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?




More information about the Python-list mailing list