[Tutor] new classes and default attribute
op73418 at mail.telepac.pt
Wed Nov 19 07:09:59 EST 2003
On Wed, 19 Nov 2003 00:20:15 -0500, you wrote:
>An instance of the builtin types will return its value when referenced.
> >>> class I(int):
> >>> i = I(123)
> >>> i
>An instance of a "normally" defined class returns type information.
> >>> class O(object):
> >>> o = O(123)
> >>> o
><__main__.O object at 0x016F0420>
>Where does this difference in behavior come from? Is there a __????__
>that can be used when defining a class to get similar behavior?
Look up the __repr__ and __str__ methods. In the above __repr__ is
>>> class Test(object):
... def __repr__(self):
... return "This is a test."
>>> a = Test()
This is a test.
With my best regards,
More information about the Tutor