Created
July 13, 2013 18:21
-
-
Save hiroakis/5991656 to your computer and use it in GitHub Desktop.
functools.partial example
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
import functools | |
def calc(x, y, mode): | |
if mode == '+': | |
return x + y | |
elif mode == '-': | |
return x - y | |
elif mode == '*': | |
return x * y | |
elif mode == '/': | |
return x / y | |
def main(): | |
print '--------- call calc ---------' | |
print 'calc(3, 1, "-") = %d' % calc(3, 1, '-') | |
print '--------- make minus(x, y) using functools.partial ---------' | |
minus = functools.partial(calc, mode='-') | |
print 'minus(3, 1) = %d' % minus(3, 1) | |
print 'minus(1, 1) = %d' % minus(1, 1) | |
print 'minus(7, 9) = %d' % minus(7, 9) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment