__new__ and __init__ - why does this work?

Ethan Furman ethan at stoneleaf.us
Wed Aug 9 16:20:36 EDT 2017


On 08/09/2017 12:59 PM, Ian Pilcher wrote:

> I do want to prevent frozenset.__init__ from being called *again* when
> an existing instance is returned, so I've decided to take this
> approach:
>
>      def __new__(cls, *args, **kwargs):
>          self = super(UniqueSet, cls).__new__(cls, *args, **kwargs)
>          self._initialized = False
>          return UniqueSet._registry.setdefault(self, self)
>
>      def __init__(self, *args, **kwargs):
>          if not self._initialized:
>              super(UniqueSet, self).__init__(self, *args, **kwargs)
>              self._initialized = True

Whom do you think is going to call __init__ a second time?

--
~Ethan~




More information about the Python-list mailing list