I have a class that knows its existing instances. Sometimes I want the class constructor to return an existing object instead of creating a new one.
class X:
def __new__(cls, arg):
i = f(arg)
if i:
return X._registry[i]
else:
return object.__new__(cls)
# more stuff here (such as __init_, _registry, etc.)
Of course, if the first branch is executed, I don't need __init__
, but it's invoked anyways. What's a good way to tell __init__
to do nothing?
I can probably just add some attribute to keep track of whether __init__
has run yet, but perhaps there's a better way?