On 08/08/2017 10:19 PM, Ian Kelly wrote:
It's initialized by the superclass call to __new__. frozenset is
immutable, and __init__ methods of immutable types generally don't do
anything (if they could, then they wouldn't be immutable), which is
why it doesn't really matter that you didn't call it. At the same
time, it generally doesn't hurt to call it, and you probably shouldn't
even have an override of __init__ here if it doesn't do anything.


Thanks for the explanation.

I'll admit that I'm a bit paranoid about the potential effects of
"re-__init__-ing" an object, at least in the general case.  What do
you think of this?

    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

It seems a bit inefficient that you create *two* sets in __new__ and
then map one of them to the other in your registry. Why not just
create the UniqueSet and then map it to itself if it's not already
registered? Something like this (untested):

def __new__(cls, *args, **kwargs):
     self = super(UniqueSet, cls).__new__(cls, *args, **kwargs)
     return UniqueSet._registry.setdefault(self, self)

That was mainly me being unfamiliar with the frozenset API (and not
overly concerned about the size of the _registry, since I expect that
there will be a very small number of entries).  Your version is much
more elegant.

Thank you!

--
========================================================================
Ian Pilcher                                         arequip...@gmail.com
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to