Question: How to only create new object instances?

Michael McGovern mmcgover at
Wed Oct 24 21:29:46 CEST 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:

------- -----
class Library:
	libList = {}
	def __init__(self,name):
		if name not in Library.libList.keys(): = name
			self.cellList = {}
			Library.libList[name] = self

	def add_cell(self,name,cell):
		if name not in self.cellList.keys():
			self.cellList[name] = cell
			print name, " already exists in ",,"\n"

class Cell:
	def __init__(self,cellName,library=None):
		if cellName in library.cellList.keys():
			print "cell already exists"
			return library.cellList[cellName]
			self.cellName = cellName
			self.library = library

>>> 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