[Tutor] new classes and default attribute

Gonçalo Rodrigues op73418 at mail.telepac.pt
Wed Nov 19 10:41:07 EST 2003


On Wed, 19 Nov 2003 10:24:40 -0500, you wrote:

[text snipped]

>That is true for a string, but doesn't work with a number.
>
> >>> repr(i)
>'123'
> >>> i.__repr__()
>'123'
> >>> i
>123
>
>I did try to look this up, but none of the "magic methods" seemed to apply.
>I do agree that __repr__ comes close.
>

Well, it's either calling __str__ or __repr__. I never quite know when
Python calls each. Go figure...

The differences between __repr__ and __str__ are indeed subtle and
have already motivated some long threads at comp.lang.py - check them
out if you're interested.

With my best regards,
G. Rodrigues

P.S: At a Python prompt I get the following:

>>> i = 123
>>> i
123
>>> str(i)
'123'
>>> repr(i)
'123'
>>> print str(i)
123
>>> print repr(i)
123
>>> print repr(i) == str(i)
True
>>> i = "123"
>>> i
'123'
>>> print i
123
>>> 



More information about the Tutor mailing list