473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What's the best way to iniatilize a function

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()
May 27 '07 #1
6 1406
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.

May 27 '07 #2
Thanks Steven, for the reply. Very helpful. I've got a lot to learn in
Python :)

Some questions:
(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.
Since it's a wrapper of a DLL or .so file, I actually need to call
mylib_exit()
to do whatever cleanup the library needs to do.
>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.
I didn't call del explicitly. I'm expecting Python to call it when
the program exits. I put a logging line in __del__() but I never
see that line printed. It seems that __del__() is not being called
even when the program exits. Any idea why?
May 28 '07 #3
Jack schrieb:
I didn't call del explicitly. I'm expecting Python to call it when
the program exits. I put a logging line in __del__() but I never
see that line printed. It seems that __del__() is not being called
even when the program exits. Any idea why?

http://effbot.org/pyfaq/my-class-def...the-object.htm

better solutions:

http://docs.python.org/ref/try.html
http://docs.python.org/whatsnew/pep-343.html

Gregor
May 28 '07 #4
On Mon, 28 May 2007 11:01:26 +0200, Gregor Horvath wrote:
Jack schrieb:
>I didn't call del explicitly. I'm expecting Python to call it when
the program exits. I put a logging line in __del__() but I never
see that line printed. It seems that __del__() is not being called
even when the program exits. Any idea why?


http://effbot.org/pyfaq/my-class-def...the-object.htm

better solutions:

http://docs.python.org/ref/try.html
http://docs.python.org/whatsnew/pep-343.html

They might be better solutions, but not for the Original Poster's problem.

The O.P. has a library that has to hang around for long time, not just a
call or two, and then needs to be closed.

try...except and with define *blocks of code*, not long-lasting libraries
with data, functions, etc. Maybe you could shoe-horn the O.P.'s problem
into a with block, but the result would surely be ugly and fragile.

--
Steven.

May 28 '07 #5
On Sun, 27 May 2007 23:20:49 -0700, Jack wrote:
Thanks Steven, for the reply. Very helpful. I've got a lot to learn in
Python :)

Some questions:
>(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.

Since it's a wrapper of a DLL or .so file, I actually need to call
mylib_exit()
to do whatever cleanup the library needs to do.
Well, there may be better ways, but I'd do something like this:
# === mylib module ===

# Exception raised if library is not initiated
class Uninitiated(Exception):
pass
def _init():
"""Private set-up code."""
global DATA, _INITIALIZED
DATA = range(100000) # or something more useful
_INITIALIZED = True

def _close():
"""Private tear-down code."""
global DATA, _INITIALIZED
del DATA, _INITIALIZED

def isinited():
"""Return True if the library is initialized, otherwise False."""
try:
_INITIALIZED; return True
except NameError:
return False

def init():
"""Public set-up code."""
if not isinited(): _init()

def close():
"""Public tear-down code."""
if isinited(): _close()

exit = close # alias for exit/close.
def func():
if isinited():
return DATA[0]
else:
raise Uninitiated("Library is not initialized")
All of the above can be modified to be in a class instead of a module.
That's especially useful if you can have multiple instances, perhaps with
different state.
>>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.

I didn't call del explicitly. I'm expecting Python to call it when
the program exits. I put a logging line in __del__() but I never
see that line printed. It seems that __del__() is not being called
even when the program exits. Any idea why?
Python tries really really hard to call instance.__del__() but there are
pathological cases where it just can't. Maybe you've found one of them.

But I'm guessing that you may have defined a __del__ method, but not
actually created an instance. If so, __del__ will never be called.

This should work:

class Foo(object):
def __del__(self):
print "All gone now"

instance = Foo()
When you exit, instance will be garbage collected and instance.__del__()
will be called. But without the instance, __del__ is not called on the
class directly.
Hope this was some help,
--
Steven.

May 28 '07 #6
Jack wrote:
>>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.

I didn't call del explicitly. I'm expecting Python to call it when
the program exits. I put a logging line in __del__() but I never
see that line printed. It seems that __del__() is not being called
even when the program exits. Any idea why?
If you make your extension available as a module (Python or C), you can use
atexit: http://docs.python.org/lib/module-atexit.html

Note: your exit handler will only be called at normal termination.

May 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
22
by: vietor.liu | last post by:
is this? int hashpjw(char* str,int size) { unsigned long h=0,g; for(unsigned char *p=(unsigned char*)str;*p;++p) { h=(h<<4)+*p; if(g=(h&0xf0000000)) {
8
by: johnsonholding | last post by:
Here is the code for a pop-up window that works in Firefox and not in IE - I get a java error or something, Here is the code : </script> <SCRIPT language="JavaScript"...
8
by: werner | last post by:
Hi! I don't want to use eval() in order to parse a user-supplied formula. What alternatives do I have? PHP has no standard functionality for tokenizing or parsing expressions in this regard. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.