Wiki

Changes between Initial Version and Version 1 of MultiArgAssign

Show
Ignore:
Timestamp:
11/04/08 07:09:45 (16 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • MultiArgAssign

    v1 v1  
     1= Multi Variable or Multi Target Assignments = 
     2 
     3Cobra supports multi variable or multi target assignment 
     4{{{ 
     5a,b,c = 10,20,30  # comma separated list of expressions 
     6 
     7# or 
     8a,b,c = [10,20,30] # list literal 
     9 
     10# or 
     11alist = [10,20,20] 
     12a,b,c = alist       # an arbitrary list variable 
     13 
     14assert a == 10 
     15assert b == 20 
     16assert c == 30 
     17}}} 
     18These are expected to give the same results as if the assignment  
     19target and source lists were unrolled and each assignment done individually 
     20{{{ 
     21a = 10 
     22b = 20 
     23c = 30 
     24 
     25a = alist[0] 
     26b = alist[1] 
     27c = alist[2] 
     28}}} 
     29 
     30The targets must be a comma separated list, the items of which are 
     31things that can be assigned a value - any of local variables,  
     32class or instance variables, properties or indexers. 
     33 
     34The assignment sources can be a single expression that resolves to a list, array or  
     35String (specifically any expression that supports an integer indexer)[[BR]] 
     36 '''or''' 
     37a comma separated stream of expressions. 
     38 
     39{{{ 
     40# unusual but acceptable - dict keyed by int in range 
     41dict = {1:'aye', 2:'bee'} 
     42a,b = dict 
     43assert a == 'aye' 
     44assert b == 'bee' 
     45 
     46# assignment to an expression list 
     47a,s,c = 99+1, 'xxx'+'y', 2*2 
     48assert a == 100 
     49assert x == 'xxxy' 
     50assert c == 4 
     51 
     52}}} 
     53 
     54The size of the target and source lists are expected to match as if the 
     55assignments were done individually. 
     56 
     57The target source values are atomic in value for the duration of the assignments. 
     58 
     59== multi Targets in a 'for loop' == 
     60  
     61Multiple targets can also be provided in a for loop covering an enumerable 
     62source. 
     63 
     64If the enumerable source is a Dictionary there may be only 2 targets and they 
     65get filled with the next key and keyValue from the dictionary for the loop 
     66block 
     67 
     68{{{ 
     69dict = {'x':'aye', 'y':'bee'} 
     70for k,v in dict 
     71    print 'key=,k, 'value=', v 
     72    assert k in '[x', 'y'] 
     73    assert v in ['aye', 'bee'] 
     74}}} 
     75 
     76Otherwise the enumerable source is expected to generate a list of items of 
     77the same size as the list of targets for each step of the enumeration 
     78{{{ 
     79for i,j in [1,2,3,4]    # error 
     80    pass 
     81 
     82for i,j in [ [1,2], [3,4]] 
     83    assert i in [1,3] 
     84    assert j in [2,4]     
     85 
     86}}} 
     87 
     88If you want to get multiple values from an enumeration generating non list 
     89(single) values the current idiom is to use a  
     90[wiki:EnumForExpr for expression] 
     91{{{ 
     92i,j = for x in [1,2,3,4] get x 
     93 
     94alist = [1,2,3,4] 
     95i,j = for x in alist get x 
     96}}}