[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: conditional import into global namespace

MRAB

3/2/2010 6:21:00 PM

mk wrote:
> Hello everyone,
>
> I have a class that is dependent on subprocess functionality. I would
> like to make it self-contained in the sense that it would import
> subprocess if it's not imported yet.
>
> What is the best way to proceed with this?
>
> I see a few possibilities:
>
> 1. do a class level import, like:
>
> class TimeSync(object):
>
> import subprocess
>
>
> 2. do an import in init, which is worse bc it's ran every time an
> instance is created:
>
> def __init__(self, shiftsec, ntpserver):
> import subprocess
>
>
> Both of those methods have disadvantage in this context, though: they
> create 'subprocess' namespace in a class or instance, respectively.
>
> Is there anyway to make it a global import?
>
The simplest solution is just import it at the top of the module.