Created
September 29, 2014 15:18
-
-
Save damnit/e8393b430ad005a7a420 to your computer and use it in GitHub Desktop.
Functional fun with python
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
| """ functional fun module. """ | |
| from __future__ import print_function | |
| import inspect | |
| from functools import partial | |
| __author__ = 'damnit <https://github.com/damnit/>' | |
| class FuncDummy(object): | |
| """ This is a class where the functional fun resides. """ | |
| def __init__(self): | |
| super(FuncDummy, self).__init__() | |
| def do_n(self, *args): | |
| """ do something with n args. | |
| >>> f = FuncDummy() | |
| >>> f.do_n(1, 2, 3, 4) | |
| 10 | |
| """ | |
| res = 0 | |
| for arg in args: | |
| res = res + arg | |
| return res | |
| def getmember(obj, memname): | |
| """ return a member for memname if exists on obj. """ | |
| for nam, func in inspect.getmembers(obj): | |
| if nam == memname: | |
| return func | |
| return None | |
| if __name__ == '__main__': | |
| f = FuncDummy() | |
| args = [1, 2, 3] | |
| fun = getmember(f, 'do_n') | |
| for arg in args: | |
| fun = partial(fun, arg) | |
| print('%d' % fun()) | |
| # vim: set ft=python ts=4 sw=4 expandtab : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment