Module imports during object instantiation

Steve Holden steve at holdenweb.com
Mon Aug 13 06:48:57 EDT 2007


Ritesh Raj Sarraf wrote:
> Hi,
> 
> I've been very confused about why this doesn't work. I mean I don't see any
> reason why this has been made not to work.
> 
> class Log:
> 
>     def __init__(self, verbose, lock = None):
> 
>         if verbose is True:
>             self.VERBOSE = True
>         else: self.VERBOSE = False
> 
Better:

	self.VERBOSE = verbose

or, if you suspect verbose might pass in a mutable value,

         self.VERBOSE  bool(verbose)

> 
>         if lock is None or lock != 1:
>             self.DispLock = False

do you want to set self.lock here? IF so, a similar improvement could be 
made (though you would still need a test to create the lock object).

>         else:
>             self.DispLock = threading.Lock()
>             self.lock = True
> 
>         if os.name == 'posix':
>            self.platform = 'posix'
>            self.color = get_colors()
> 
>         elif os.name in ['nt', 'dos']:
>             self.platform = 'microsoft'
>             
>             try:
>                 import SomeModule
>             except ImportError:
>                 self.Set_Flag = None
>             
>             if self.Set_Flag is not None:
>                 self.color = SomeModule.get_colors_windows()
> 
>         else:
>             self.platform = None
>             self.color = None
> 
> When I create an object the "import" part never gets executed. Is there a
> reason behind it ?
> I mean I'd like to keep my class as independent as I want. So that when
> later I need to use it somewhere else, I don't need to know if it depends
> on any modules.
> 
> Currently, the way I'm left is to globally go and import the module and set
> a flag there.
> 
What's leading you to conclude the import isn't being executed? You 
realise, I trust, that the module's code will only be executed on the 
first call to __init__()?

You are right in assuming that __init__() is called once per instance 
created, and it's legitimate to make an import conditional in the way 
you have because of the "execute code only once" behavior - if the 
module is already in sys.modules then it won't be re-imported, the 
existing one will be used.

Having said all that, I still don't see why you can't just put the 
try/except at the top level of your code and have color be a global. Why 
repeat the attempted import and the computation for each object you 
create? Alternatively, do it at the class level, so it's only executed 
once when the class is declared?

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list