[Tutor] HELP: subclass of int
Kent Johnson
kent37 at tds.net
Wed Mar 9 06:19:24 CET 2005
Shidai Liu wrote:
> Hi all,
>
> I'll sum up a question as following:
>
> def int5():
> '''return 5'''
> return 5
>
> class my_int(int):
> def __init__(self):
> self.id = int5()
> int.__init__(self, self.id) # FIXME: this line doesn't work
>
> the above code act like this:
>
>>>>I = my_int()
>>>>I
>
> 0
>
> I want it to be like this:
>
>>>>I = my_int()
>>>>I
>
> 5
>
> Please, if you know a solution, help me out!
When you subclass an immutable type like int (or float, string, ...) you have to define __new__
instead of __init__. See this page for an example and some explanation:
http://www.python.org/2.2.3/descrintro.html#__new__
Kent
>
More information about the Tutor
mailing list