Wiki

Changes between Version 1 and Version 2 of MultiArgAssign

Show
Ignore:
Timestamp:
11/30/08 09:32:57 (16 years ago)
Author:
Chuck
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • MultiArgAssign

    v1 v2  
    33Cobra supports multi variable or multi target assignment 
    44{{{ 
    5 a,b,c = 10,20,30  # comma separated list of expressions 
     5a, b, c = 10, 20, 30  # comma separated list of expressions 
    66 
    77# or 
    8 a,b,c = [10,20,30] # list literal 
     8a, b, c = [10, 20, 30] # list literal 
    99 
    1010# or 
    11 alist = [10,20,20] 
    12 a,b,c = alist       # an arbitrary list variable 
     11alist = [10, 20, 20] 
     12a, b, c = alist       # an arbitrary list variable 
    1313 
    1414assert a == 10 
     
    4040# unusual but acceptable - dict keyed by int in range 
    4141dict = {1:'aye', 2:'bee'} 
    42 a,b = dict 
     42a, b = dict 
    4343assert a == 'aye' 
    4444assert b == 'bee' 
    4545 
    4646# assignment to an expression list 
    47 a,s,c = 99+1, 'xxx'+'y', 2*2 
     47a, x, c = 99+1, 'xxx'+'y', 2*2 
    4848assert a == 100 
    4949assert x == 'xxxy' 
    5050assert c == 4 
    51  
    5251}}} 
    5352 
     
    5756The target source values are atomic in value for the duration of the assignments. 
    5857 
    59 == multi Targets in a 'for loop' == 
     58== Multi Targets in a 'for loop' == 
    6059  
    6160Multiple targets can also be provided in a for loop covering an enumerable 
     
    6867{{{ 
    6968dict = {'x':'aye', 'y':'bee'} 
    70 for k,v in dict 
    71     print 'key=,k, 'value=', v 
     69for k, v in dict 
     70    print 'key=', k, 'value=', v 
    7271    assert k in '[x', 'y'] 
    7372    assert v in ['aye', 'bee'] 
     
    7776the same size as the list of targets for each step of the enumeration 
    7877{{{ 
    79 for i,j in [1,2,3,4]    # error 
     78for i, j in [1, 2, 3, 4]    # error 
    8079    pass 
    8180 
    82 for i,j in [ [1,2], [3,4]] 
    83     assert i in [1,3] 
    84     assert j in [2,4]     
    85  
     81for i, j in [[1, 2], [3, 4]] 
     82    assert i in [1, 3] 
     83    assert j in [2, 4] 
    8684}}} 
    8785 
     
    9088[wiki:EnumForExpr for expression] 
    9189{{{ 
    92 i,j = for x in [1,2,3,4] get x 
     90i, j = for x in [1, 2, 3, 4] get x 
    9391 
    94 alist = [1,2,3,4] 
    95 i,j = for x in alist get x 
     92alist = [1, 2, 3, 4] 
     93i, j = for x in alist get x 
    9694}}}