Is 'isinstance()' the right thing?

Ralf Juengling juenglin at informatik.uni-freiburg.de
Tue May 28 09:13:41 EDT 2002


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message news:<MHDz8.34856$n4.7400679 at newsc.telia.net>...
> 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>

But even docs can't tell me, what I want.
Snippet from the operator module documentation:

"
isSequenceType(o)
Returns true if the object o supports the sequence protocol. This
returns true for all objects which define sequence methods in C, and
for all instance objects. Warning: There is no reliable way to test if
an instance supports the complete sequence interface since the
interface itself is ill-defined. This makes this test less useful than
it otherwise might be.
"

Why is the sequence interface 'ill-defined'? This is just a matter of 
agreement on a definition, I guess?

BTW: The Python docs use 'interface' and 'protocol' synonymic?

Ralf



More information about the Python-list mailing list