Why I have problem creating a class inheriting from str (or also from int)
class C(str):
def __init__(self, a, b):
str.__init__(self,a)
self.b = b
C("a", "B")
TypeError: str() takes at most 1 argument (2 given)
the same happens if I try to use int instead of str, but it works with custom classes. I need to use __new__ instead of __init__? why?
Since
__init__is called after the object is constructed, it is too late to modify the value for immutable types. Note that__new__is a classmethod, so I have called the first parameterclsSee here for more information