Skip to content

Instantly share code, notes, and snippets.

@adammck
Created October 25, 2010 22:10
Show Gist options
  • Save adammck/645896 to your computer and use it in GitHub Desktop.
Save adammck/645896 to your computer and use it in GitHub Desktop.
Call with do-what-I-mean args
def call_with_dwim_args(callable_, args):
"""
Call and return ``callable_`` with *do-what-I-mean* ``args``. More
precisely:
| if ``args`` is a dict, call ``callable_(**args)``
| if ``args`` is a list, call ``callable_(*args)``
| otherwise, call ``callable_(args)``
The usual Pythonic solution of testing ``iter(args)`` and catching
TypeError is not used here, since ``basestring`` is (apparently)
iterable, which is not what I usually mean.
"""
if isinstance(args, dict):
return callable_(**args)
elif isinstance(args, (list, tuple)):
return callable_(*args)
else:
return callable_(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment