Skip to content
Advertisement

When I inherit from frozenset, I get TypeError: object.__init__() takes exactly one argument (the instance to initialize)

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:

JavaScript

However, when I try to create an instance of B, I get an error:

JavaScript

What’s going on and how to fix this?

Advertisement

Answer

For technical reasons, for immutable types like frozenset and tuple, 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:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement