I have a set of functions to wrap a library. For example, mylib_init() mylib_func() mylib_exit()
or handle = mylib_init() mylib_func(handle) mylib_exit(handle) In order to call mylib_func(), mylib_init() has to be called once. When it's done, or when program exits, mylib_exit() should be called once to free resources. I can list all three functions in a module and let the application manage the init call and exit call. Or, better, to have the wrapper function manage these calls. I'm currently using a singleton class (see below.) It seems to work fine. My questions here are: 1. every time I call the function: MyLib().func() part of the object creation code is called, at least to check if there is an existing instance of the class, then return it. So it may not be very efficient. Is there a better way? 2. what's the right way to call mylib_exit()? I put it in __del__(self) but it is not being called in my simple test. STATUS_UNINITIALIZED = 0 STATUS_INITIALIZED = 1 STATUS_ERROR = 2 class MyLib (object): instance = None status = STATUS_UNINITIALIZED def __new__(cls, *args, **kargs): if cls.instance is None: cls.instance = object.__new__(cls, *args, **kargs) return cls.instance def __init__(self): if self.status == STATUS_UNINITIALIZED: mylib_init() self.status = STATUS_INITIALIZED def func(self): return mylib_func() def __del__(self): mylib_exit() -- http://mail.python.org/mailman/listinfo/python-list