PySequence_Check

Fredrik Lundh fredrik at pythonware.com
Wed Jun 30 10:09:30 CEST 1999


Reuben Sumner wrote:
> How can I do the equivalent of a PySequence_Check in Python?
> GvR told me that the question was illdefined (if I explained it correctly)
> and to ask here.  It seems that some of the functions in abstract.h
> are available from python (len(), callable()) and others are not
> (PySequence_Check, PyNumber_Check).

import operator
operator.isSequenceType
operator.isNumberType
operator.isMappingType

not documented, and should probably be considered as
deprecated (see replies from Paul and Tim for some back-
ground).

> What am I missing?  I have a nested structure of lists and tuple and I
> just want to know when I have bottomed out.  I can check for tuple or
> list indivually but that is ugly and inflexible.

> I could just try indexing and see if there was an exception but
> that is not much better.

given that Python has no support for declared inter-
faces, that's a reasonable approach.  works just fine,
and adds no extra overhead.

on the other hand, it doesn't work if you have strings
in there.  but that's true for most other approaches
as well:

>>> operator.isSequenceType("spam")
1
>>> operator.isSequenceType("spam"[0])
1
>>> operator.isSequenceType("spam"[0][0])
1
>>> operator.isSequenceType("spam"[0][0][0])
1
>>> operator.isSequenceType("spam"[0][0][0][0])
1
>>> operator.isSequenceType("spam"[0][0][0][0][0])
1

</F>





More information about the Python-list mailing list