Customize the effect of enumerate()?

D

Dustan

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)

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.
 
P

Paul Rubin

Dustan said:
Can I make enumerate(myObject) act differently?
No.

Why the funny behavior, you ask? For my class A, it doesn't make sense
to number everything the standard programming way.

Add an enumerate method to the class then, that does what you want.
Maybe dict.iteritems would be a better example to follow.
 
D

Dustan

Paul said:
Add an enumerate method to the class then, that does what you want.
Maybe dict.iteritems would be a better example to follow.

That's what I thought. Thanks anyway!
 
S

Simon Forman

Dustan said:
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?
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'.
 
S

Steve Holden

Simon said:
Dustan said:
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.
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
 
D

Dustan

Simon said:
Dustan said:
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?

I'm not 'expecting' anything to reach that clause, but it is a good
catch-all if I forget something or have a bug somewhere.
You can write your own enumerate function and then bind that to the
name 'enumerate'.

Except that my program is supposed to be treated as a module with tools
to do certain things. I certainly can't control whether a 3rd party
programmer uses "import myModule" or "from myModule import *".

I haven't gotten around to doing it yet, but I'm pretty sure I'm
planning on taking Paul Rubin's course of action - make a method
(iteritems or similar) that will enumerate correctly.
 
F

Fredrik Lundh

Dustan said:
Except that my program is supposed to be treated as a module with tools
to do certain things. I certainly can't control whether a 3rd party
programmer uses "import myModule" or "from myModule import *".

anything can happen if people use "from import *" in the wrong way, so that's
not much of an argument, really.

</F>
 
S

Steven D'Aprano

Dustan said:
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?

Possible a NaN value? Maybe a class instance with strange comparison
methods?

Personally, I don't like the error message. "Invalid index" doesn't really
tell the caller what went wrong and why it is an invalid index. If I were
programming that defensively, I'd write:

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:
print repr(item)
raise ThisCanNeverHappenError("Congratulations! You've discovered "
"a bug that can't possibly occur. Contact the program author for "
"your reward.")

I know some programmers hate "Can't happen" tests and error messages, but
if you are going to test for events that can't possibly happen, at least
deal with the impossible explicitly!
 
D

Dustan

Fredrik said:
anything can happen if people use "from import *" in the wrong way, so that's
not much of an argument, really.

</F>

My argument was that if they use "import myModule", overriding
enumerate() wouldn't work. So "from myModule import *" would work
nicely, but not the former. Given that, I'm not getting your rebuttal,
or whatever it is.

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?

Possible a NaN value? Maybe a class instance with strange comparison
methods?

Personally, I don't like the error message. "Invalid index" doesn't really
tell the caller what went wrong and why it is an invalid index. If I were
programming that defensively, I'd write:

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:
print repr(item)
raise ThisCanNeverHappenError("Congratulations! You've discovered "
"a bug that can't possibly occur. Contact the program author for "
"your reward.")

I know some programmers hate "Can't happen" tests and error messages, but
if you are going to test for events that can't possibly happen, at least
deal with the impossible explicitly!

I certainly can't argue with that logic; I might even go so far as to
agree with you and start raising impossible errors with this kind of
explicitness.

What reward should I offer? ;-)
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top