>>> class x:... y = 0... >>> for x.y in range(5):... print x.y,... 0 1 2 3 4>>> >>> x = [None]>>> for x[0] in range(5):... print x,... [0] [1] [2] [3] [4]>>>
Another interesting one is:def f(a, (b, c)): print a, b, cf(1, [2, 3]) # --> 1 2 3f(1, 2) # --> TypeError: unpack non-sequence
To go along with Ian sharing the not-well-known fact that tuple unpacking works in more places than as an lvalue, I thought I'd share one of my favorite subtle things to do:>>> x = [1]>>> y, = x>>> print y1
I usually spell that 'y ,= x' for consistency with other augmented assignment operations :-)
ewwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Another interesting one is:
ReplyDeletedef f(a, (b, c)): print a, b, c
f(1, [2, 3]) # --> 1 2 3
f(1, 2) # --> TypeError: unpack non-sequence
To go along with Ian sharing the not-well-known fact that tuple unpacking works in more places than as an lvalue, I thought I'd share one of my favorite subtle things to do:
ReplyDelete>>> x = [1]
>>> y, = x
>>> print y
1
I usually spell that 'y ,= x' for consistency with other augmented assignment operations :-)
ReplyDeleteewwwwwwwwwwwwwwwwwwwwwwwwwwwwww
ReplyDelete