Namespace issue

7stud bbxx789_05ss at yahoo.com
Wed May 23 15:31:58 EDT 2007


On May 23, 12:20 pm, Ritesh Raj Sarraf <r... at researchut.com> 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()


--output:---
importing...
[a bunch of files]


[a bunch of files]




More information about the Python-list mailing list