I am reading a blog post on examples of overriding __new__
in python. However, I don't understand the following example:
class LimitedInstances(object):
_instances = [] # Keep track of instance reference
limit = 5
def __new__(cls, *args, **kwargs):
if not len(cls._instances) <= cls.limit:
raise RuntimeError, "Count not create instance. Limit %s reached" % cls.limit
instance = object.__new__(cls, *args, **kwargs)
cls._instances.append(instance)
return instance
def __del__(self):
# Remove instance from _instances
self._instance.remove(self)
Wouldn't calling object.__new__(cls, *args, **kwargs)
cause infinite recursion? In addition, I saw that __new__
actually happens before an object exists. Then what is stored in cls
exactly?