Simple Python class questions

Ulrich Eckhardt eckhardt at satorlaser.com
Thu Jun 19 08:21:46 EDT 2008


John Dann wrote:
> Let's say I define the class in a module called comms.py. The class
> isn't really going to inherit from any other class (except presumably
> in the most primitive base-class sense, which is presumably automatic
> and implicit in using the class keyword). Let's call the class
> serial_link. So in comms.py I have:
> 
> class serial_link:
>     def __init__(self):
>         Try
>             Import serial # the pyserial library

Stop, this can't work. Other than VB, Python actually is case sensitive, so
you must write 'try' and not 'Try' and also 'import' and not 'Import'.
Further, many (all?) statements that cause an indention are usually
terminated with a colon, so like with 'class ..:' and 'def ..:' you also
must use 'try:' and not just 'try'. Fix all these and try again, I guess
this will already help a lot.

One more thing: you are abusing exceptions. Typically, in such a short
program you only have one try-except pair in the main entry function and
all other code only throws the exceptions. In particular the __init__
function of a class should always signal errors using exceptions. However,
this is not a strict yes/no question but rather a stylistic one.


Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list