[Python-bugs-list] [ python-Bugs-474737 ] Problem overriding int.__init__()

noreply@sourceforge.net noreply@sourceforge.net
Thu, 25 Oct 2001 05:43:54 -0700


Bugs item #474737, was opened at 2001-10-24 21:48
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=474737&group_id=5470

Category: Type/class unification
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Mark J (average)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Problem overriding int.__init__()

Initial Comment:
Given:

class I(int):
    def __init__(self, init_value, extra_param="test"):
        print extra_param
        int.__init__(self, init_value)

Python 2.2b1 (#1, Oct 19 2001, 23:11:09) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on
linux2
>>> i = I(1)   #fine
test           #yes, new constructor used
>>> i
1              #value set correctly           
>>> i = I(2, "oops")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> i = I(3, dummy="oops")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: dummy is an invalid keyword argument for
this function
>>>

Thanks,

Mark

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-25 05:43

Message:
Logged In: YES 
user_id=6380

The constructor signature is shared by __new__ and __init__;
you have to override both to accept the new argument. Add

def __new__(cls, i=0, *args):
    return int.__new__(cls, i)

(I promise we'll document all this in enough detail
eventually!)

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=474737&group_id=5470