Calvin Spealman wrote:
I don't think it would be unreasonable to consider either 1) making
functools.partial picklable (I don't know how feasible this is)
It's not only feasible, but quite easy and, I think, useful. A
"partial" instance is a simple triplet of (function, args, kwds), and it
can be pickled as such. For example:
>>> import copy_reg, functools
>>> def _reconstruct_partial(f, args, kwds):
... return functools.partial(f, *args, **(kwds or {}))
...
>>> def _reduce_partial(p):
... return _reconstruct_partial, (p.func, p.args, p.keywords)
...
>>> copy_reg.pickle(functools.partial, _reduce_partial)
Test:
>>> import operator, cPickle as cp
>>> p = functools.partial(operator.add, 3)
>>> p(10)
13
>>> cp.dumps(p)
'c__main__\n_reconstruct_partial\np1\n(coperator\nadd\np2\n(I3\ntp3\nNtRp4\n.'
>>> p2 = cp.loads(_)
>>> p2(10)
13
Iedally this should be implemented in the functools.partial object itself.
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com