[Python-Dev] operator.is*Type
Raymond Hettinger
python at rcn.com
Wed Feb 22 22:18:58 CET 2006
> But given :
>
> True True Instance w getitem <type 'instance'>
> True True NewStyle Instance w getitem <class '__main__.cng'>
> True True [] <class UserList.UserList at 0x00F11B70>
> True True {} <type 'instance'>
>
> (Last one is UserDict)
>
> I can't conceive of circumstances where this is useful without duck
> typing *as well*.
Yawn. Give it up. For user defined instances, these functions can only
discriminate between the presence or absence of __getitem__. If you're trying
to distinguish between sequences and mappings for instances, you're own your own
with duck-typing. Since there is no mandatory mapping or sequence API, the
operator module functions cannot add more checks without getting some false
negatives (your original example is a case in point).
Use the function as-is and add your own isinstance checks for your own personal
definition of what makes a mapping a mapping and what makes a sequence a
sequence. Or better yet, stop designing APIs that require you to differentiate
things that aren't really different ;-)
Raymond
More information about the Python-Dev
mailing list