Hmm... An idea: if a,b==c,d:

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Nov 18 19:03:20 EST 2002


>>>>> "Richard" == Richard Dillingham <shadowlord13_1 at yahoo.com> writes:

    Richard> As I have asked everyone else that brings up that point,
    Richard> then why does the following fail (Using Python 2.2.2)?

    if a,b==c,d: print a
      File "<input>", line 1
        if a,b==c,d: print a

To simplify note that the following also fails

if 1,2:
    pass

Now, I am no expert in the subtleties of the python language syntax,
but here is my read of the language reference (defns below from The
Python Reference Manual v2.2)


  The `if' statement
  ==================
  
     The `if' statement is used for conditional execution:
  
       if_stmt:        "if" expression ":" suite
                      ("elif" expression ":" suite)*
                      ["else" ":" suite]


The operative part of this is 'expression'.  


  Expression lists
  ================
  
       expression_list:      expression ("," expression)* [","]
  
   An expression list containing at least one comma yields a tuple.
  The length of the tuple is the number of expressions in the list.  The
  expressions are evaluated from left to right.


In the example
if 1,2:
  pass

1,2 is an expression list, *not* an expression


  Parenthesized forms
  -------------------
  
     A parenthesized form is an optional expression list enclosed in
  parentheses:
  
       parenth_form:      "(" [expression_list] ")"
  
     A parenthesized expression list yields whatever that expression list
  yields: if the list contains at least one comma, it yields a tuple;
  otherwise, it yields the single expression that makes up the expression
  list.

if (1,2):
  pass 

This is ok because (1,2) is a parenthesized form.  1,2 creates a
tuple, but from the language definition it is a expression_list, not
an expression.  (1,2) is an expression, so it can be used with the if
statement, while the expression list cannot.

Language mavens feel free to correct me.

John Hunter




More information about the Python-list mailing list