Is 'isinstance()' the right thing?

Fredrik Lundh fredrik at pythonware.com
Tue Apr 30 17:06:52 EDT 2002


Erik Max Francis wrote:

> *BUT*, these (eg. isSequenceType) work only for C extension types,
> > right?
>
> No, they work with any object:
>
> >>> import operator
> >>> operator.isSequenceType([1, 2, 3])
> 1
> >>> class MyList(list): pass
> ...
> >>> operator.isSequenceType(MyList())
> 1

define "work" :

>>> class NotAList: pass
...
>>> operator.isSequenceType(NotAList())
1
>>> for item in NotAList(): print item
Traceback (most recent call last):
AttributeError: NotAList instance has no attribute '__getitem__'

(the isSequenceType predicate checks if the object's type
implements the C-level __getitem__ slot.  that's not always
what you want...)

</F>





More information about the Python-list mailing list