[Tutor] __getitem__
Alan Gauld
alan.gauld at freenet.co.uk
Tue Jan 17 00:54:22 CET 2006
> Sorry!
>
> X = stepper()
> X.data = "Spam"
Aha! The fog in my brain clears.
>> > >>> class stepper:
>> >... def __getitem__(self, i):
>> >... return self.data[i]
>> >...
>> > >>> 'p' in X
>> >True
>> >
>> >What does 'in' have to do with indexing?
To find out if 'p' is in X Python has to step through the data in X.
I'm not sure that your getitem is being called, it may just be a string
search operation. But I suspect it may assume the data is an iterator
and repeatedly call next() till it gets a match or runs out of data.
In that case getitem may well get called. a simple print statement
in getitem will find out...
Alan G.
More information about the Tutor
mailing list