no __len__ attribute in NodeList or the list base?

Hrvoje Niksic hniksic at
Thu Nov 15 10:09:36 CET 2007

sndive at writes:

> i extract class object from an instance of NodeList (
> like so
> PyObject *pclass = PyObject_GetAttrString( nodelistinsance,
> "__class__");
> but
>  PyObject_GetAttrString(pclass, "__len__")
> returns NULL.

Are you sure about that?

>>> import xml.dom.minicompat
>>> xml.dom.minicompat.NodeList
<class 'xml.dom.minicompat.NodeList'>
>>> xml.dom.minicompat.NodeList.__len__
<slot wrapper '__len__' of 'list' objects>
>>> xml.dom.minicompat.NodeList()
>>> _.__class__
<class 'xml.dom.minicompat.NodeList'>
>>> _.__len__
<slot wrapper '__len__' of 'list' objects>

> to explain that endless loop i have to assume __len__ is defined
> in the NodeList base(list that is)
> but i don't see __len__ anywhere in listobject.c!!!

The list object has __len__:

>>> list.__len__
<slot wrapper '__len__' of 'list' objects>

It just gets defined in a different way, through a C structure of
function pointers defining sequence operations.  (See where
list_length is defined and used.)

More information about the Python-list mailing list