Skip to content

Instantly share code, notes, and snippets.

@christabor
Created January 27, 2017 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christabor/7ca55fa8434c17d53d8d3e4e905d9655 to your computer and use it in GitHub Desktop.
Save christabor/7ca55fa8434c17d53d8d3e4e905d9655 to your computer and use it in GitHub Desktop.
More collections - tuples
"""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