Changes between Version 1 and Version 2 of MultiArgAssign
- Timestamp:
- 11/30/08 09:32:57 (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
MultiArgAssign
v1 v2 3 3 Cobra supports multi variable or multi target assignment 4 4 {{{ 5 a, b,c = 10,20,30 # comma separated list of expressions5 a, b, c = 10, 20, 30 # comma separated list of expressions 6 6 7 7 # or 8 a, b,c = [10,20,30] # list literal8 a, b, c = [10, 20, 30] # list literal 9 9 10 10 # or 11 alist = [10, 20,20]12 a, b,c = alist # an arbitrary list variable11 alist = [10, 20, 20] 12 a, b, c = alist # an arbitrary list variable 13 13 14 14 assert a == 10 … … 40 40 # unusual but acceptable - dict keyed by int in range 41 41 dict = {1:'aye', 2:'bee'} 42 a, b = dict42 a, b = dict 43 43 assert a == 'aye' 44 44 assert b == 'bee' 45 45 46 46 # assignment to an expression list 47 a, s,c = 99+1, 'xxx'+'y', 2*247 a, x, c = 99+1, 'xxx'+'y', 2*2 48 48 assert a == 100 49 49 assert x == 'xxxy' 50 50 assert c == 4 51 52 51 }}} 53 52 … … 57 56 The target source values are atomic in value for the duration of the assignments. 58 57 59 == multi Targets in a 'for loop' ==58 == Multi Targets in a 'for loop' == 60 59 61 60 Multiple targets can also be provided in a for loop covering an enumerable … … 68 67 {{{ 69 68 dict = {'x':'aye', 'y':'bee'} 70 for k, v in dict71 print 'key= ,k, 'value=', v69 for k, v in dict 70 print 'key=', k, 'value=', v 72 71 assert k in '[x', 'y'] 73 72 assert v in ['aye', 'bee'] … … 77 76 the same size as the list of targets for each step of the enumeration 78 77 {{{ 79 for i, j in [1,2,3,4] # error78 for i, j in [1, 2, 3, 4] # error 80 79 pass 81 80 82 for i,j in [ [1,2], [3,4]] 83 assert i in [1,3] 84 assert j in [2,4] 85 81 for i, j in [[1, 2], [3, 4]] 82 assert i in [1, 3] 83 assert j in [2, 4] 86 84 }}} 87 85 … … 90 88 [wiki:EnumForExpr for expression] 91 89 {{{ 92 i, j = for x in [1,2,3,4] get x90 i, j = for x in [1, 2, 3, 4] get x 93 91 94 alist = [1, 2,3,4]95 i, j = for x in alist get x92 alist = [1, 2, 3, 4] 93 i, j = for x in alist get x 96 94 }}}