Created
January 27, 2017 23:16
-
-
Save christabor/7ca55fa8434c17d53d8d3e4e905d9655 to your computer and use it in GitHub Desktop.
More collections - tuples
This file contains 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
"""Specialized tuples.""" | |
def n_tuple(argcount): | |
"""Return a new function to generate a tuple. | |
But only if the specified arity is met. | |
""" | |
def _tuple(*someargs): | |
if len(someargs) != argcount: | |
raise ValueError( | |
'{}-tuple requires arity of {}, ' | |
'but arity of {} was passed'.format( | |
argcount, | |
argcount, | |
len(someargs) | |
)) | |
return tuple(someargs) | |
return _tuple | |
def tuple3(*args): | |
"""Create a 3-tuple.""" | |
return n_tuple(3)(*args) | |
def tuple2(*args): | |
"""Create a 2-tuple.""" | |
return n_tuple(2)(*args) | |
assert tuple2('foo', 'bar') == ('foo', 'bar') | |
tuple2('33', 'foo', 'bar') == ('foo', 'bar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment