Missing __length_hint__ in __getitem__ iterator

G

Giovanni Bajo

Hello,

given the following object:
.... def __getitem__(self, idx):
.... if idx >= 10: raise IndexError
.... return idx
.... def __len__(self):
.... return 10
....

I noticed that the iterator that Python constructs:
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__iter__', '__len__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'next']

does not have a __length_hint__ method.

Is this just a missing optimization, or there is a deep semantic reason for
which a __length_hint__ could not be constructed out of the __len__ result?
 
P

Peter Otten

Giovanni said:
Hello,

given the following object:
... def __getitem__(self, idx):
... if idx >= 10: raise IndexError
... return idx
... def __len__(self):
... return 10
...

I noticed that the iterator that Python constructs:
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__iter__', '__len__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'next']

does not have a __length_hint__ method.

Is this just a missing optimization, or there is a deep semantic reason
for which a __length_hint__ could not be constructed out of the __len__
result?

It's there, just not doctored into the dir() output:
.... def __getitem__(self, index): return index
.... def __len__(self): return 42
....42

Peter
 
G

Giovanni Bajo

Peter said:
It's there, just not doctored into the dir() output:

... def __getitem__(self, index): return index
... def __len__(self): return 42
...
42

Peter

Ah well, actually it's there also in dir(), you just need to use Python 2.5
of course ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top