Question: How to only create new object instances?
Michael McGovern
mmcgover at pender.ee.upenn.edu
Wed Oct 24 15:29:46 EDT 2001
I am trying to create new cells only when they don't exist in a given library
of cells. In the case where they already exist in the supplied library, I
would like the "new" cell to refer to the exisiting cell in the library.
Here is a snippet of code:
------- Cell.py -----
class Library:
libList = {}
def __init__(self,name):
if name not in Library.libList.keys():
self.name = name
self.cellList = {}
Library.libList[name] = self
def add_cell(self,name,cell):
if name not in self.cellList.keys():
self.cellList[name] = cell
else:
print name, " already exists in ", self.name,"\n"
class Cell:
def __init__(self,cellName,library=None):
if cellName in library.cellList.keys():
print "cell already exists"
return library.cellList[cellName]
else:
self.cellName = cellName
self.library = library
self.library.add_cell(self.cellName,self)
---------------
>>> from Cell import *
>>> lib1 = Library(name='lib1')
>>> i1 = Cell(cellName='A1',library=lib1)
>>> i2 = Cell(cellName='A1',library=lib1)
cell already exists
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __init__() should return None
>>>
How can I accomplish this?
Thanks in advance for the help.
Scott M.
mmcgover at pender dot ee dot upenn dot edu
More information about the Python-list
mailing list