only one instance of each named type

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-10-07 16:01:02 +02:00
parent b505eb1af8
commit a7a086e29b

View file

@ -46,6 +46,16 @@ class Type(object):
return os.listdir(base_path)
_instances = {}
def __new__(cls, *args, **kwargs):
"""only one instance of each named type may exist"""
# name is second argument
name = args[1]
if not name in cls._instances:
instance = super(Type, cls).__new__(cls, *args, **kwargs)
cls._instances[name] = instance
return cls._instances[name]
def __init__(self, base_path, name):
self._base_path = base_path
self.name = name
@ -64,7 +74,6 @@ class Type(object):
self.__required_parameters = None
self.__optional_parameters = None
def __repr__(self):
return '<Type %s>' % self.name