[Pyrex] Quick 'len()' speedup for Pyrex

Phillip J. Eby pje at telecommunity.com
Thu Apr 22 22:40:45 CEST 2004


At 09:19 PM 4/22/04 +0200, Jiba wrote:
>On Thu, 22 Apr 2004 14:05:56 -0400
>"Phillip J. Eby" <pje at telecommunity.com> wrote:
>
> > If your Pyrex code uses the 'len()' builtin, here's a handy quick fix
> > you can pop in at the beginning of your code:
> >
> > cdef extern from "Python.h":
> >      int len "PySequence_Length" (object o) except -1
>
>Does this work with user-defined len(), such as :
>
>class MyClass:
>         def __len__(self): return 3
>
>???

I believe so.  However, it does occur to me it might not work for objects 
that only implement the "mapping length" protocol and not the "sequence 
length" protocol.  So using PyObject_Length instead should work with anything.





More information about the Pyrex mailing list