[Tutor] new classes and default attribute

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Nov 19 11:02:27 EST 2003


>  >>> 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?

I assume because the inherited str() and repr() methods are
different between objects and integers. The integer returns
a number for both cases whereas the object returns a type
description.

Alan G.




More information about the Tutor mailing list