Created
April 10, 2015 14:13
-
-
Save Averroes/f55093bcc1092be7089f to your computer and use it in GitHub Desktop.
unpack a fixed number of elements from iterables of arbitrary length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # example.py | |
| # | |
| # Unpacking of tagged tuples of varying sizes | |
| records = [ | |
| ('foo', 1, 2), | |
| ('bar', 'hello'), | |
| ('foo', 3, 4), | |
| ] | |
| def do_foo(x,y): | |
| print('foo', x, y) | |
| def do_bar(s): | |
| print('bar', s) | |
| for tag, *args in records: | |
| if tag == 'foo': | |
| do_foo(*args) | |
| elif tag == 'bar': | |
| do_bar(*args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment