[Tutor] how do you make this a better code?

Kent Johnson kent37 at tds.net
Fri Sep 5 03:42:07 CEST 2008


On Thu, Sep 4, 2008 at 9:00 PM, Roy Khristopher Bayot
<roybayot at gmail.com> wrote:

> class CamHandle(object):
>     def __init__(self):
>         import os
>         w = os.system('gphoto2 --auto-detect > CamHandleLog.txt')
>         test = os.system('gphoto2 --capture-image >> CamHandleLog.txt')
>
>     def captureImage(self):
>         import os
>         x = os.system('gphoto2 --capture-image >> CamHandleLog.txt')
>
> Now when I import the file to the interpreter and instantiate the class, it
> works. Each function works. I havent done any error handling or exception
> handling yet but it works for now. However, I am bothered with the import
> statements in each function.

The usual method is to put the import statement at the beginning of
the module. Then the imported module has global (module) scope rather
than function scope.

Kent


More information about the Tutor mailing list