[Tutor] new classes and default attribute

Gonçalo Rodrigues 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):
>... 	pass
>...
> >>> i = I(123)
> >>> i
>123
>
>An instance of a "normally" defined class returns type information.
> >>> class O(object):
>... 	pass
>...
> >>> 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
used, e.g.

>>> class Test(object):
... 	def __repr__(self):
... 		return "This is a test."
... 	
>>> a = Test()
>>> a
This is a test.
>>> 

With my best regards,
G. Rodrigues



More information about the Tutor mailing list