Martin v. Löwis wrote:
> 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.

Probably doesn't matter for my particular use, but it certainly wouldn't 
hurt to do it the careful way.  Thanks.

Is new.instancemethod basically equivalent to calling __get__?  That is, 
would the following two unpickle_instancemethod functions do the same thing?

     def unpickle_instancemethod(func_name, cls, obj):
         return cls.__dict__[func_name].__get__(obj, cls)

     def unpickle_instancemethod(func_name, cls, obj):
         return new.instancemethod(cls.__dict__[func_name], obj, cls)

Thanks again,

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to