I have an application that creates a lot of objects that are initialized by
parsing a string. In many cases, the initalization strings are duplicated
many times, which results in a lot of duplicate objects being created at
runtime. I originally utilized a factory to generate new object instances,
but it was a pain to write a new factory for each object tpye. So I created
a class called UniqueObject, and subclassed all my objects with it.
import weakref
class UniqueObject(ob ject):
__instances = weakref.WeakVal ueDictionary()
def __new__(cls, source):
return cls.__instances .setdefault(sou rce,
super(UniqueObj ect, cls).__new__(cl s, source))
def getSource(self) :
return self.__class__. __instances.key s()[
self.__class__. __instances.val ues().index(sel f)]
This insured that duplicate strings would not result in the creation of
duplicate objects, and I could us "is" for comparisons instead of having to
define an "__eq__".
[color=blue][color=green][color=darkred]
>>> class X(UniqueObject) :[/color][/color][/color]
def __init__(self, source):
pass
[color=blue][color=green][color=darkred]
>>> x1, x2, x3, x4 = X('spam'), X('spam'), X('eggs'), X('spam')
>>> x1 is x2[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x2 is x3[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> x1 != x3[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x3 is not x4[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x1 in (x4,)[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x1.getSource()[/color][/color][/color]
'spam'
Am I reinventing the wheel here? Is there a way to accomplish the same
thing in a built-in, or more Pythonic way?
Chris
parsing a string. In many cases, the initalization strings are duplicated
many times, which results in a lot of duplicate objects being created at
runtime. I originally utilized a factory to generate new object instances,
but it was a pain to write a new factory for each object tpye. So I created
a class called UniqueObject, and subclassed all my objects with it.
import weakref
class UniqueObject(ob ject):
__instances = weakref.WeakVal ueDictionary()
def __new__(cls, source):
return cls.__instances .setdefault(sou rce,
super(UniqueObj ect, cls).__new__(cl s, source))
def getSource(self) :
return self.__class__. __instances.key s()[
self.__class__. __instances.val ues().index(sel f)]
This insured that duplicate strings would not result in the creation of
duplicate objects, and I could us "is" for comparisons instead of having to
define an "__eq__".
[color=blue][color=green][color=darkred]
>>> class X(UniqueObject) :[/color][/color][/color]
def __init__(self, source):
pass
[color=blue][color=green][color=darkred]
>>> x1, x2, x3, x4 = X('spam'), X('spam'), X('eggs'), X('spam')
>>> x1 is x2[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x2 is x3[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> x1 != x3[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x3 is not x4[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x1 in (x4,)[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> x1.getSource()[/color][/color][/color]
'spam'
Am I reinventing the wheel here? Is there a way to accomplish the same
thing in a built-in, or more Pythonic way?
Chris