I am reading the book Mastering Object-Oriented Python. In the part of book where it talks about the __new__
method and immutable objects, it says something like the __new__()
method is used to extend the immutable classes where the __init__()
method can't easily be overridden. It also gives an example as below
class Float_Fail(float):
def __init__(self, value, unit):
super().__init__(value)
self.unit = unit
f = Float_Fail(6.5, "knots")
TypeError Traceback (most recent call last)
<ipython-input-2-9511e6ca03be> in <module>()
----> 1 f = Float_Fail(6.5, "knots")
TypeError: float() takes at most 1 argument (2 given)
I'm just curious why float gives this error. I mean, we can initialize a float like this:
f = float(1.1)
Doesn't it mean the __init__()
method of float
class takes the number as it's second argument. If not, how is the initialization of an immutable class implemented in Python?