pickle and instancemethod objects

"Martin v. Löwis" martin at v.loewis.de
Wed Sep 13 11:55:04 EDT 2006


Steven Bethard schrieb:
> Does this approach seem sound?  Am I going to run into some weird
> problems doing it this way?

It's good, but I think rebuilding the object through
new.instancemethod should be even better.

py> class A:
...   def f(self):print "A"
...
py> class B(A):
...   def f(self):print "B"
...
py> b=B()
py> b.f
<bound method B.f of <__main__.B instance at 0xa7d728cc>>
py> x = new.instancemethod(A.__dict__['f'], b, A)
py> x
<bound method A.f of <__main__.B instance at 0xa7d728cc>>
py> x()
A
py> b.f()
B
py> x.im_func.__name__,x.im_class,x.im_self
('f', <class __main__.A at 0xa7d7002c>, <__main__.B instance at 0xa7d728cc>)

On unpickling x, you'ld get x.(B.f), not x.(A.f) with your
approach.

Not sure it matters much.

Regards,
Martin



More information about the Python-list mailing list