Working with method-wrapper objects

Steven Bethard steven.bethard at gmail.com
Thu Apr 28 22:08:09 EDT 2005


Dr. Peer Griebel wrote:
>> Why has [].__str__ a different type than object.__str__?
>> Why is object.__str__ a routine while object().__str__ not?

Well, I don't know why inspect.isroutine does what it does, but if you 
really need to detect these, can you do something like:

py> MethodWrapperType = type(object().__str__)
py> type([].__str__) == MethodWrapperType
True

This is basically all the types module does for similar types.  (Take a 
look -- it's written in Python.)

> Some more investigation shows that also the module "types" is not 
> universally usable. E.g. InstanceType is only usabel for instances of 
> old classes. How do I test for instances of new classes?

isinstance(obj, object)

All new-style classes are subclasses of object (even type!)

STeVe



More information about the Python-list mailing list