What's the best way to iniatilize a function

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sun May 27 19:40:02 EDT 2007


On Sun, 27 May 2007 10:51:46 -0700, Jack wrote:

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


Sounds like a module to me.

Write it as a module. Python will call your initialization code, once, the
first time you import it. Python will handle the garbage collection of
your resources when your module goes away. Modules are automatically
singletons.


# mylib wrapper(?) module
# Initialize a bunch of data
DATA = range(1000000)

def func():
    return DATA[0] # or something useful...


Note that mylib.func() doesn't need to check that it is initialized,
because if it isn't, it can't be called! That is to say, if some of your
initialization code fails, an exception will be raised and the module will
not be imported.

Note two gotchas: 

(1) Python can automatically free most data structures and close open
files, but if your needs are more sophisticated, this approach may not be
suitable.

(2) Module imports are cached. Calling "import mylib" imports the module,
but calling "del mylib" doesn't free it because of the cache.

Calling "del mylib; del sys.modules['mylib']" should remove the cache,
allowing Python's normal garbage collection to free your resources, but
note the above caveats regarding the types of resources that Python can
automatically free.



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


Why do you care that it is a singleton? Surely the important thing is that
all the instances share the same state, not that there is only one
instance?

The easiest way to ensure all instances share the same state is to put all
the data you care about into class attributes instead of instance
attributes:


class MyLib(object):
    DATA = range(100000) # initialized when the class is defined
    def func(self):
        return self.DATA[0]

Note that there are no __init__ or __new__ methods needed.

If you don't want the (trivial) expense of creating new instances just to
call the func method, use a class method that doesn't need an instance:

class MyLib(object):
    DATA = range(100000) # initialized when the class is defined
    @classmethod
    def func(cls):
        return cls.DATA[0]


See also the source to the random module for another way to expose a
class-based interface as if it were a set of functions.



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

Yes. The caller should save the instance and reuse it, the same as any
other expensive instance:

obj = MyLib()
obj.func()
obj.func()


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

instance.__del__ is only called when there are no references to the
instance. 

instance = MyLib()
another = instance
data = [None, 1, instance]
something = {"key": instance}
del another  # __del__ will not be called, but name "another" is gone
del instance # __del__ still not called, but name "instance" is gone
data = []    # __del__ still not called
something.clear()  # now __del__ will be called!


It isn't easy to keep track of all the places you might have a reference
to something, but Python can do it much better than you can.


-- 
Steven.




More information about the Python-list mailing list