With file xx.py:
class Foo(object):
def __init__(self, x):
self.x = x
def __long__(self):
return long(self.x)
def __float__(self):
return float(self.x)
y = Foo(22)
print '%d' % y
print '%d' % y
Interactive mode:
$ python -V
Python 2.6.7
$ python -i xx.py
22
22
>>>
TypeError: int() argument must be a string or a number, not 'Foo'
>>> '%d' % y
'22'
>>> '%d' % y
TypeError: int() argument must be a string or a number, not 'Foo'
>>> '%d' % y
'22'
>>> '%d' % y
TypeError: int() argument must be a string or a number, not 'Foo'
Why does it alternate between printing '22' and raising a TypeError?
Why does it raise an error on the first carriage return (with no input)?
Thanks.
raise(Exception()). It prints the sameTypeErrorerror message and not, say, aNameErroror anExceptionmessage.int(y)consistently prints aTypeError(except in the first instance as noted above).