Simon Forman wrote:
> Dustan wrote:
> 
>>Can I make enumerate(myObject) act differently?
>>
>>class A(object):
>>      def __getitem__(self, item):
>>              if item > 0:
>>                  return self.sequence[item-1]
>>              elif item < 0:
>>                  return self.sequence[item]
>>              elif item == 0:
>>                  raise IndexError, "Index 0 is not valid."
>>              else:
>>                  raise IndexError, "Invalid Index."
>>      def __iter__(self): return iter(self.sequence)
> 
> 
> That final else clause is a little funny...    What kind of indices are
> you expecting that will be neither less than zero, greater than zero,
> or equal to zero?
> 
Good defensive programming albeit of a somewhat extreme nature. Should 
one of the tests be removed at a later date the else clause will trap 
occurrences of the no-longer handled case.

> 
>>Why the funny behavior, you ask? For my class A, it doesn't make sense
>>to number everything the standard programming way. Of course, if
>>someone uses enumerate, it's going to number the items the same way as
>>ever. Is there any way to modify that behavior, any special function to
>>set? There doesn't appear to be, according to the docs, but it never
>>hurts to make sure.
> 
> 
> You can write your own enumerate function and then bind that to the
> name 'enumerate'.
> 
Yes, but if you do you had better make sure that it gives the standard 
behavior for normal uses.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to