class return another instance

Max Erickson maxerickson at gmail.com
Tue Aug 8 10:16:29 EDT 2006


"Keith" <python at cgcreator.com> wrote:

>  
> 
> Any help would be great.
> 
>  
> 
> Cheers,
> 
> Keith
> 
> 

Do you absolutely need the functionality to be in the __init__ method, 
or does something like the following work:


>>> IDs={}
>>> class ID: pass

>>> def factory(ident):
	if ident in IDs:
		instance=IDs[ident]
	else:
		instance=ID()
		IDs[ident]=instance
	return instance

>>> g=factory(1)
>>> h=factory(2)
>>> i=factory(1)
>>> g,h,i
(<__main__.ID instance at 0x00A45FD0>, <__main__.ID instance at 
0x00A4B918>, <__main__.ID instance at 0x00A45FD0>)
>>> 


max




More information about the Python-list mailing list