[Level 2] The sample of functools.partial() in Python.
#!/bin/env python
import functools
try:
print int('10') ## equiv: print int('10', base=10)
print int('10', 2) ## equiv: print int('10', base=2)
int2 = functools.partial(int, 2) ## no default bonding parameter.
print int2('10') ## fail, must assign the value of base
int2 = functools.partial(int, base=2) ## set default base to 2
print int2('10') ## equiv: print int2('10', base=2)
print int2('10', 10) ## fail, TypeError: keyword parameter 'base' was given by position and by name
print int2('10', base=10)
Wish this helps. regards,
Stanley Huang
Comments
Post a Comment