How can I make a class that can be converted into an int?

Mikael Olofsson mikael at
Mon Oct 2 17:12:58 CEST 2006

Matthew Wilson wrote:
> What are the internal methods that I need to define on any class so that
> this code can work?
> c = C("three")
> i = int(c) # i is 3

 From Python Reference Manual, section 3.4.7 Emulating numeric types:

__complex__( self)
__int__( self)
__long__( self)
__float__( self)
     Called to implement the built-in functions complex(), int(), 
long(), and float(). Should return a value of the appropriate type.


More information about the Python-list mailing list