netimen a écrit :
(snip)
OK, I have implemented Bruno Desthuilliers example. But there is
another question: can I having a method determine if it is an instance
of given class. So:
As the name imply, a method is usually, well, an instance of type
'method' !-)
class Obj(object):
(snip)
class Foo(object):
pass
Foo.meth = Obj()
## in some another place of code
if isinstance(Foo.meth, Obj): # doesn't work because type(Foo.meth) is
now 'instancemethod'
Indeed. I suppose what you want is to know if the callable wrapped by
the method is an instance of Obj ?
Can I determine that?
Yeps. Test the im_func attribute of the method:
if isinstance(Foo.meth.im_func, Obj): print "yadda"
But you'd better put this in a try/except block, because a callable
attribute of a class is not necessarily a method - and so it may not
have an im_func attribute.
Also, may I ask why you want to check this ? Not that it's necessarily
wrong, but real use case for typechecking are pretty rare.
--
http://mail.python.org/mailman/listinfo/python-list