question about imports in a class
Diez B. Roggisch
deets at nospam.web.de
Mon Dec 7 16:57:22 EST 2009
J schrieb:
> On Mon, Dec 7, 2009 at 16:13, J <dreadpiratejeff at gmail.com> wrote:
>
>> But why does importing in the init not make os available to every
>> other function in the class? Do I have to import OS into every
>> function like this:
>>
>> class ClassA():
>>
>> def func1(self):
>> import os
>>
>> def func2(self):
>> import os
>
> A little more education and playing around and I'm still not quite
> sure how to do this...
>
> for the class i'm writing, I want to import os, sys and wmi globally
> for the class...
> if I put the import at the beginning of the class, it just dawned on
> me that perhaps I still have to explicitly call the function by class:
>
> sysinfo.py
>
> class myclass():
> import os
> def findDMIDecode(self):
> for r,d,f in myclass.os.walk(args)
>
> is that correct?
It is correct in the sense that it works, but it is by no means good
practice.
>
> How about if I have two classes in sysinfo.py and want to import os
> globally for BOTH classes to use? Same thing?
import os
class A():
...
class B():
...
That's it.
Diez
More information about the Python-list
mailing list