Skip to content

Instantly share code, notes, and snippets.

@maxwillzq
Created October 23, 2013 01:14
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 maxwillzq/7110904 to your computer and use it in GitHub Desktop.
Save maxwillzq/7110904 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 01 17:24:03 2013
@author: qzhang
"""
import functools
def myfunc(a, b=2):
"""Docstring for myfunc()."""
print ' called myfunc with:', (a, b)
return
def show_details(name, f, is_partial=False):
"""Show details of a callable object."""
print '%s:' % name
print ' object:', f
if not is_partial:
print ' __name__:', f.__name__
if is_partial:
print ' func:', f.func
print ' args:', f.args
print ' keywords:', f.keywords
return
show_details('myfunc', myfunc)
myfunc('a', 3)
print
# Set a different default value for 'b', but require
# the caller to provide 'a'.
p1 = functools.partial(myfunc, b=4)
show_details('partial with named default', p1, True)
p1('passing a')
p1('override b', b=5)
print
# Set default values for both 'a' and 'b'.
p2 = functools.partial(myfunc, 'default a', b=99)
show_details('partial with defaults', p2, True)
p2()
p2(b='override b')
print
print 'Insufficient arguments:'
p1()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment