Created
October 25, 2010 22:10
-
-
Save adammck/645896 to your computer and use it in GitHub Desktop.
Call with do-what-I-mean args
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
| 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