Simple Python class questions

John Dann news at prodata.co.uk
Fri Jun 20 07:19:32 EDT 2008


Many thanks for the further comments:

On Thu, 19 Jun 2008 21:24:31 -0400, Terry Reedy <tjreedy at udel.edu>
wrote:

>> 	def __init__(self):
>> 		Try
>> 			Import serial # the pyserial library

>The import should be at module level.  You only want to do it once, not 
>for every link.  And if the import fails, you should find out right 
>away.  

Yes I was wondering about that, but I wasn't clear about when 'body'
code (ie not contained within a def block) in the module might run
under Python. So it seemed to be safer to place the import statement
inside the 'constructor' to get the earliest warning of non-visibility
of pyserial. But you seem to be implying that the body code will run
when the class is instantiated - have I understood that right? It
surely doesn't run when the module containing the class is imported
into the main module - does it?? It would certainly make life easier
to place the import in the body of the module.

I think that some of the other points hinge on the this one, so let me
get my understanding straight on that first!

>
>I guess you learned by now why cut/paste/edit-down is superior to 
>re-typing ;-)

Well I know what you mean, but actually in this instance my Python
environment is a non-networked laptop , so no easy way to cut and
paste to a networked PC! (Actually the laptop does have an Ethernet
chip in but bizarrely the driver somehow manages to kill my ADSL
connection at the exchange or ISP, which takes hours to reset so I
take care not to use this option. But learning Linux/Python is a
useful role for this otherwise defunct PC)

JGD



More information about the Python-list mailing list