How to control the creation of an instance?

lialie lialie at gmail.com
Sun Jun 3 00:31:48 EDT 2007


Hi,

suppose i have a free_object list[Sample1, Smaple2....]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.

any way to do this?

I do some work as follows:

class Sample(object):
used_object = []
free_object = []

def __init__(self, *args, **kwds):
pass


def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj

######## still get a new instance :(


def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True




More information about the Python-list mailing list