Is this a valid use of 'import'?

Frank Millman frank at chagford.com
Tue Jul 22 17:12:06 CEST 2008


Hi all

I am familiar enough with the normal use of 'import'. However, I have
found a use for it which seems effective, but I have not seen it used
like this before, so I am not sure if there are any downsides.

I know that when a module is imported the first time, it is
'executed'. This normally entails setting up constants, classes,
functions, etc, that you want to make available to the importer.

In this particular case, when it is executed, it does a whole lot
more. It reads in some parameters, establishes a socket connection,
starts a thread, and starts monitoring the socket using select.select.
It also exposes some functions that disguise the complexity of reading
from and writing to the socket.

This enables me to write a 'client' program that look like this -

---------------------------
from Utils.client import *

connect(userid='frank',pwd='')
cust = getRecord(
            company='chagford',table='ArCustomers',
            column='CustNo',value='A001')
print cust
close()
---------------------------

As you can see, it makes writing a client program very easy.

Are there any problems with this approach?

Frank Millman



More information about the Python-list mailing list