[Tutor] question about where to import

Erik Price erikprice@mac.com
Mon, 1 Apr 2002 21:47:23 -0500


I have a quick question about where import statements should go.  Most 
scripts feature them at the top of the script, or module, or whatever 
code container is calling the import statement.  Between the Stocks 
class we discussed earlier this weekend and another tutorial I found, 
I'm finally starting to "get" the idea behind OO -- it's not that hard 
to understand, but my impression is that it can be very tricky to 
efficiently come up with a flexible and reuseable class, and that it is 
in the "planning" stage that all the hard work takes place.

But I have a question.  Appended to this email is a code snippet, a 
class actually.  I didn't write it, it's from a tutorial.  In the class, 
the "time" module and the "gmtime" module are both imported for use.  
But the point at which they are imported intrigues me -- they are not 
imported at the top of the module, nor at the top of the class 
definition, but rather at the top of one of the class method 
definitions.  Is there some reason that the author would choose to put 
their import statement in a method definition?  I am probably wrong, but 
doesn't that mean that these modules are imported every time the method 
is called?  and if this is true, then doesn't this get expensive if you 
are creating many instances of the class?

If this is not just some laziness on the part of the tutorial-writer 
(and I'm not saying it is!), then can someone just explain why you would 
do it this way and not just import once at the top of the script -- if I 
am not mistaken, these functions should be available to any instance of 
the class if it is done this way too.

TIA,

-- Erik





# each Clock object is initialized with offsets
# indicating the difference between GMT and local time

class Clock:

   # constructor
   def __init__(self, offsetSign, offsetH, offsetM, city):
     # set variables to store timezone offset
     # from GMT, in hours and minutes, and city name
     self.offsetSign = offsetSign
     self.offsetH = offsetH
     self.offsetM = offsetM
     self.city = city

     # print message
     print 'Clock created'

     # method to display current time, given offsets
     def display(self):

       # use the gmtime() function to convert local to GMT
       # returns an array
       from time import time, gmtime

       self.GMTTime = gmtime(time())

       self.seconds = self.GMTTime[5]
       self.minutes = self.GMTTime[4]
       self.hours = self.GMTTime[3]

       # calculate time
       if (self.offsetsign == '+'):
         # city time is ahead of GMT
         self.minutes = self.minutes + self.offsetM

         if (self.minutes > 60):
           self.minutes = self.minutes - 60
           self.hours = self.hours + 1

         self.hours = self.hours + self.offsetH

         if (self.hours >= 24):
           self.hours = self.hours - 24

       else:
         # city time is behind GMT
         self.seconds = 60 - self.seconds
         self.minutes = self.minutes - self.offsetM

         if (self.minutes < 0):
           self.minutes = self.minutes + 60
           self.hours = self.hours -1

         self.hours = self.hours - self.offsetH

         if (self.hours < 0):
           self.hours = 24 + self.hours

       # make it look pretty and display it
       self.localTime = str(self.hours) + ":" + str(self.minutes) + ":" + 
str(self.seconds)
       print "Local time in " + self.city + " is " + self.localTime