Created
October 23, 2013 01:14
-
-
Save maxwillzq/7110904 to your computer and use it in GitHub Desktop.
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
# -*- 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) | |
# 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) | |
# 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 'Insufficient arguments:' | |
p1() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment