On May 23, 12:20 pm, Ritesh Raj Sarraf <[EMAIL PROTECTED]> wrote: > As per my understanding, the bad part is that on every call of the method > FetchData(), an import would be done. > > To not let that happen, I can put the import into __init__(). But when I put > in there, I get a NameError saying that my_module is not available even > though it got imported. > All I noticed is that the import has to be part of the method else I end up > getting a NameError. But always importing my_module is also not good.
How about something like this: class Dog(object): myimport = None def __init__(self): if not Dog.myimport: print "importing..." import os Dog.myimport = os def test(self): print Dog.myimport.listdir("./") d = Dog() d.test() print print d2 = Dog() d2.test() -- http://mail.python.org/mailman/listinfo/python-list