0

With the below code, I am getting the following error:

class C4(int):
    def __init__(self,name1,name2):
        self.name1=name1
        self.name2=name2

When above code is executed, I get the error:

C4_inst=C4(3,6)
TypeError: int() can't convert non-string with explicit base

When I only use one parameter for__init__, I don't see the error. Can you please shed some light on why python would try to convert the parameters to int when the objective here is to instantiate C4 with 3,6. I am trying to make C4 as the subclass to "int", so that i can use the operators defined for "int" type in Class C4.

2
  • 1
    why do you inherit from int, what are you trying to achive? Commented Oct 31, 2015 at 19:49
  • Well i was just playing around operator overloading and wanted to see if i can just inherit from "int" to overload "add". Commented Oct 31, 2015 at 22:51

1 Answer 1

2

The base int type is immutable, so its arguments are interpreted in its __new__ method, rather than __init__. Since you haven't defined your own __new__ method, the base class's version is being called, and it doesn't understand your arguments. An int can be initialized with two arguments, but the first must be a string and the second must be an integer (the base).

Try adding a __new__ method that initializes the integer value properly:

class C4(int):
    def __new__(cls, name1, name2):
        return super(cls, C4).__new__(cls)  # let int.__new__ create a new value.
    def __init__(self, name1, name2):
        self.name1=name1
        self.name2=name2

Note that since we're not passing any arguments to int.__new__, our new instance will be equal to 0 (and since we don't have a __repr__ or __str__ method, it will also print out as 0). If you want the object to behave like one of its int arguments (or some other non-zero value), you'll want to pass that along in the call to int.__new__.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the clarification. It worked with the above suggestion. Didn't realize that the new() method in superclass "int" was intercepting the "C4_inst" instance creation. Further digging into new made it clear and actually just passing the params with the right format ("3",10) took care of it even without any "new" overloading in my class C4.
It wasn't entirely clear what the variables you were getting passed were supposed to mean, so I left my answer generic. If you just want to preserve the arguments, you can get away with only overloading __init__ (though you probably want to add default arguments so all the int call types work). Note that it's sort of difficult to work with int subclasses, since any operator you don't overload will return a regular int instance rather than an instance of your custom class. It's often easier to write your class from scratch, encapsulating an int rather than inheriting from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.