I want to inherit from frozenset and change the constructor. What I actually want to do is to make a singleton fronzeset, but instead here I'll provide a simplified example:
class B(frozenset):
def __init__(self):
super().__init__([1, 2, 3])
However, when I try to create an instance of B, I get an error:
B() # TypeError: object.__init__() takes exactly one argument (the instance to initialize)
What's going on and how to fix this?
For technical reasons, for immutable types like
frozensetandtuple, etc. overriding their__init__to set an initial value is the wrong way to go. This is partly to do with the fact that for the object to be created it has to know how large it's going to be based on the input argument.You need to override
__new__instead. For example: