Mailing List Archive

PySequence_Check
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).

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.

Reuben
PySequence_Check [ In reply to ]
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.
>...
> 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.

Hi, Reuben.

This latter is probably our best bet. Python in general has no way of
declaring that an object acts as a sequence or mapping. Therefore there
can be no way of querying whether an object acts as a sequence or
mapping. In general, Python has no way of declaring that an object "acts
as" anything at all (other examples would include a set, a widget, a web
browser). There is no concept of interface or protocol except in the C
API. I think that the goal is to define the concept in general before
applying it specifically to sequences, mappings, file objects and other
"built-in" protocols.

My discussion with Guido on this issue began here:

<Pine.SUN.3.91.980824165825.9662F-100000@cito.uwaterloo.ca>

or you can do a deja search with "IsMappingType"

There are some tricky issues about what this interface stuff all means in
a dynamically typed language.

The constant re-occurence of discussions like this have lead to the
development of a special interest group:

http://www.python.org/sigs/types-sig/

--
Paul Prescod - ISOGEN Consulting Engineer speaking for only himself
http://itrc.uwaterloo.ca/~papresco

"The new revolutionaries believe the time has come for an aggressive
move against our oppressors. We have established a solid beachhead
on Friday. We now intend to fight vigorously for 'casual Thursdays.'
-- who says America's revolutionary spirit is dead?
PySequence_Check [ In reply to ]
From: Paul Prescod <paul@prescod.net>

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.
>...
> 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.

Hi, Reuben.

This latter is probably our best bet. Python in general has no way of
declaring that an object acts as a sequence or mapping. Therefore there
can be no way of querying whether an object acts as a sequence or
mapping. In general, Python has no way of declaring that an object "acts
as" anything at all (other examples would include a set, a widget, a web
browser). There is no concept of interface or protocol except in the C
API. I think that the goal is to define the concept in general before
applying it specifically to sequences, mappings, file objects and other
"built-in" protocols.

My discussion with Guido on this issue began here:

<Pine.SUN.3.91.980824165825.9662F-100000@cito.uwaterloo.ca>

or you can do a deja search with "IsMappingType"

There are some tricky issues about what this interface stuff all means in
a dynamically typed language.

The constant re-occurence of discussions like this have lead to the
development of a special interest group:

http://www.python.org/sigs/types-sig/

--
Paul Prescod - ISOGEN Consulting Engineer speaking for only himself
http://itrc.uwaterloo.ca/~papresco

"The new revolutionaries believe the time has come for an aggressive
move against our oppressors. We have established a solid beachhead
on Friday. We now intend to fight vigorously for 'casual Thursdays.'
-- who says America's revolutionary spirit is dead?
PySequence_Check [ In reply to ]
From: "Tim Peters" <tim_one@email.msn.com>

[Reuben Sumner]
> 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).
>
> What am I missing?

For starters, unquestioning faith in Guido's pronouncements <wink>.

> I have a nested structure of lists and tuple and I just want to know
> when I have bottomed out.

Then why not check for that directly and be done with it?

> I can check for tuple or list indivually but that is ugly and
> inflexible.

Not to mention simple, obvious and unsurprising <wink>.

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

OK, you tell me: what *is* a sequence? Answer that, and you'll know how to
check for it <0.5 wink>. Before answering too fast, note that mapping types
can be indexed without raising an exception, and so can strings. Do you
want your nest of "lists and tuples" *not* to "bottom out" at a string?
Probably not -- but if so, you're not really interested in testing whether
it's a sequence! Note too that user-defined classes can support the
__getitem__ protocol without supporting the __getslice__ protocol, or vice
versa. Are either of those sequences, or do they need to support both?
Whichever way you answer, half the world will disagree.

I think that, in most cases, when people say "sequence": (A) it's
ill-defined <wink>; and, (B) after five rounds of tedious questioning they
end up deciding they mean "responds to slicing but isn't a string". So
there you go:

def issequence(x):
try:
x[0:0]
except:
return 0
return type(x) is not type("")

may-not-match-what-you-mean-by-"sequence"-ly y'rs - tim
PySequence_Check [ In reply to ]
[Reuben Sumner]
> 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).
>
> What am I missing?

For starters, unquestioning faith in Guido's pronouncements <wink>.

> I have a nested structure of lists and tuple and I just want to know
> when I have bottomed out.

Then why not check for that directly and be done with it?

> I can check for tuple or list indivually but that is ugly and
> inflexible.

Not to mention simple, obvious and unsurprising <wink>.

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

OK, you tell me: what *is* a sequence? Answer that, and you'll know how to
check for it <0.5 wink>. Before answering too fast, note that mapping types
can be indexed without raising an exception, and so can strings. Do you
want your nest of "lists and tuples" *not* to "bottom out" at a string?
Probably not -- but if so, you're not really interested in testing whether
it's a sequence! Note too that user-defined classes can support the
__getitem__ protocol without supporting the __getslice__ protocol, or vice
versa. Are either of those sequences, or do they need to support both?
Whichever way you answer, half the world will disagree.

I think that, in most cases, when people say "sequence": (A) it's
ill-defined <wink>; and, (B) after five rounds of tedious questioning they
end up deciding they mean "responds to slicing but isn't a string". So
there you go:

def issequence(x):
try:
x[0:0]
except:
return 0
return type(x) is not type("")

may-not-match-what-you-mean-by-"sequence"-ly y'rs - tim
PySequence_Check [ In reply to ]
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>
PySequence_Check [ In reply to ]
From: "Fredrik Lundh" <fredrik@pythonware.com>

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>
PySequence_Check [ In reply to ]
Tim Peters <tim_one@email.msn.com> wrote:
: [Reuben Sumner]
:> 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).
:>
:> What am I missing?

: For starters, unquestioning faith in Guido's pronouncements <wink>.

:> I have a nested structure of lists and tuple and I just want to know
:> when I have bottomed out.

: Then why not check for that directly and be done with it?

:> I can check for tuple or list indivually but that is ugly and
:> inflexible.

: Not to mention simple, obvious and unsurprising <wink>.

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

: OK, you tell me: what *is* a sequence? Answer that, and you'll know how to
: check for it <0.5 wink>. Before answering too fast, note that mapping types
: can be indexed without raising an exception, and so can strings. Do you
: want your nest of "lists and tuples" *not* to "bottom out" at a string?
: Probably not -- but if so, you're not really interested in testing whether
: it's a sequence! Note too that user-defined classes can support the
: __getitem__ protocol without supporting the __getslice__ protocol, or vice
: versa. Are either of those sequences, or do they need to support both?
: Whichever way you answer, half the world will disagree.

: I think that, in most cases, when people say "sequence": (A) it's
: ill-defined <wink>; and, (B) after five rounds of tedious questioning they
: end up deciding they mean "responds to slicing but isn't a string". So
: there you go:

: def issequence(x):
: try:
: x[0:0]
: except:
: return 0
: return type(x) is not type("")

My basis (in my own programming) has been:
import types, UserList
def issequence(x):
return (isinstance(x, types.ListType) or
isinstance(x, types.TupleType) or
isinstance(x, UserList.UserList))

It is very restrictive in some cases, but if I want a user defined
sequence, I usually _try_ to make one from UserList.

-Arcege
PySequence_Check [ In reply to ]
From: "Michael P. Reilly" <arcege@shore.net>

Tim Peters <tim_one@email.msn.com> wrote:
: [Reuben Sumner]
:> 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).
:>
:> What am I missing?

: For starters, unquestioning faith in Guido's pronouncements <wink>.

:> I have a nested structure of lists and tuple and I just want to know
:> when I have bottomed out.

: Then why not check for that directly and be done with it?

:> I can check for tuple or list indivually but that is ugly and
:> inflexible.

: Not to mention simple, obvious and unsurprising <wink>.

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

: OK, you tell me: what *is* a sequence? Answer that, and you'll know how to
: check for it <0.5 wink>. Before answering too fast, note that mapping types
: can be indexed without raising an exception, and so can strings. Do you
: want your nest of "lists and tuples" *not* to "bottom out" at a string?
: Probably not -- but if so, you're not really interested in testing whether
: it's a sequence! Note too that user-defined classes can support the
: __getitem__ protocol without supporting the __getslice__ protocol, or vice
: versa. Are either of those sequences, or do they need to support both?
: Whichever way you answer, half the world will disagree.

: I think that, in most cases, when people say "sequence": (A) it's
: ill-defined <wink>; and, (B) after five rounds of tedious questioning they
: end up deciding they mean "responds to slicing but isn't a string". So
: there you go:

: def issequence(x):
: try:
: x[0:0]
: except:
: return 0
: return type(x) is not type("")

My basis (in my own programming) has been:
import types, UserList
def issequence(x):
return (isinstance(x, types.ListType) or
isinstance(x, types.TupleType) or
isinstance(x, UserList.UserList))

It is very restrictive in some cases, but if I want a user defined
sequence, I usually _try_ to make one from UserList.

-Arcege