Question: How to only create new object instances?

sebastien s.keim at laposte.net
Thu Oct 25 09:35:23 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.

Maybe something like this could work

class _Cell:
 	def __init__(self,cellName,library=None):		
 			self.cellName = cellName
 			self.library = library
 			self.library.add_cell(self.cellName,self)

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

In Python 2.2 the natural way to achieve this would probably be to use
the __new__ class method and to construct the new instance 'by hand'.



More information about the Python-list mailing list