How do you bind a parsing option into a reusable importer function?

partial creates a callable with some positional or keyword arguments pre-bound.

Python example and interpretation

Binding base sixteen gives several hexadecimal IDs one converter. Preserve source strings for lookup since numeric conversion removes leading zeros.

from functools import partial
as_hex=partial(int,base=16)
[as_hex(s) for s in ['0A','10','FF']]

Result [10, 16, 255]

中文

怎样把固定的解析参数绑定成可复用的导入函数?

partial 创建新的可调用对象,预先保存部分位置参数或关键字参数。

Python 算例与适用条件

固定 base=16 后,多个十六进制编号可复用同一转换函数。保留原字符串用于回查,因为转换会去掉前导零。

from functools import partial
as_hex=partial(int,base=16)
[as_hex(s) for s in ['0A','10','FF']]

结果 [10, 16, 255]

References / 参考来源

Keep reading / 继续阅读