[SciPy-dev] Adding to an array with __radd__()
Ed Schofield
schofield at ftw.at
Tue Nov 1 09:37:38 CST 2005
Robert Cimrman wrote:
>I have just tested with:
>
>PyObject *isSequence( PyObject *input ) {
>
> if (PySequence_Check( input )) {
> return( PyBool_FromLong( 1 ) );
> } else {
> return( PyBool_FromLong( 0 ) );
> }
>}
>
>print isSequence( [] )
>print isSequence( (1,) )
>print isSequence( {} )
>print isSequence( scipy.sparse.dok_matrix( scipy.array( [[1,2,3]] ) ) )
>
>and got (Python 2.4.2):
>
>True
>True
>False
>True
>
>... so the problem is not in PySequence_Check(). The DOK matrix inherits
>not only from dict, but also from spmatrix. Could this cause such a
>behaviour??
>
>
Ah, well done. So a dict doesn't define PySequence_Check(). But,
according to my tests, neither does an instance of the spmatrix base
class. Instead it seems that any class that inherits from a dict does
define PySequence_Check():
class E(dict):
pass
d = {}
e = E()
print isSequence(d)
print isSequence(e)
gives
False
True
Very strange. The same is true with a class derived from UserDict. Can
someone explain this behaviour?
Meanwhile it seems that any Python class instance that defines the
__getitem__ method has PySequence_Check() true by default. Another
cruel trick! Can this be overridden without using C?
I've found this (somewhat old) comment by GvR, admitting that the
sequence protocol is poorly defined:
http://mail.python.org/pipermail/python-checkins/2001-September/021227.html
Perhaps another PEP is in order? Meanwhile I'll reapply my patch to
handle sequences more cautiously...
-- Ed
More information about the Scipy-dev
mailing list