Skip to content

Instantly share code, notes, and snippets.

@hiroakis
Created July 13, 2013 18:21
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 hiroakis/5991656 to your computer and use it in GitHub Desktop.
Save hiroakis/5991656 to your computer and use it in GitHub Desktop.
functools.partial example
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