Is this a valid use of 'import'?

Fredrik Lundh fredrik at pythonware.com
Tue Jul 22 11:31:15 EDT 2008


Frank Millman wrote:

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

besides being fragile and not scalable and not thread-safe and 
incompatible with introspection tools and utterly surprising for people 
used to normal Python behaviour, and only marginally easier to write 
than, say:

     from Utils.client import Client

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

and some other problems that I cannot think of right now, you mean?

</F>




More information about the Python-list mailing list