__getitem__, __getslice__ question for python 2.2

Andrew Dalke dalke at dalkescientific.com
Wed May 15 08:44:45 EDT 2002


Raymond Hettinger:
>if x != int(x):  print 'expected an integer'

Others pointed out the ValueError.  When I've gone this
route, I also made sure I used the result of int, so I knew
that I had an integer from there onwards, as in

try:
  converted_x = int(x)
except ValueError:
  raise TypeError("expected an integer")
if converted_x != x:
  raise TypeError("expected an integer")
x = converted_x

In other words, it's the integer which equals x (if it exists)
rather than x if x can be represented as an integer.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list