Floating point bug?
Duncan Booth
duncan.booth at invalid.invalid
Thu Feb 14 10:34:48 EST 2008
Christian Heimes <lists at cheimes.de> wrote:
> Bruno Desthuilliers wrote:
>> I Must have miss something...
>
> Yeah, You have missed the beginning of the third sentence: "The
tp_print
> slot is not available from Python code". The tp_print slot is only
> available in C code and is part of the C definition of a type. Hence
tp_
> as type.
It isn't easy to come up with an example which actually demonstrates
that print doesn't just call str, but after a little playing around I
managed it:
>>> class mystr(str):
def __str__(self):
return mystr('oops')
>>> s = mystr('aargh')
>>> s
'aargh'
>>> str(s)
'oops'
>>> print s
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
print s
RuntimeError: maximum recursion depth exceeded
>>>
Alternatively:
>>> class mystr(str):
def __str__(self):
if self=='oops':
return 'you printed me!'
return mystr('oops')
>>> s = mystr('aargh')
>>> str(s)
'oops'
>>> str(str(s))
'you printed me!'
>>> print s
and on that last line idle locks until you restart the shell. I can't
immediately see why; the command line interpreter is fine so it seems
just to be an idle problem:
>>> class mystr(str):
... def __str__(self):
... if self=='oops':
... return 'you printed me!'
... return mystr('oops')
...
>>> s = mystr('aargh')
>>> s
'aargh'
>>> str(s)
'oops'
>>> print s
you printed me!
>>>
More information about the Python-list
mailing list