Module imports during object instantiation

Ritesh Raj Sarraf rrs at researchut.com
Mon Aug 13 20:10:27 CEST 2007


Bruno Desthuilliers wrote:

> Ritesh Raj Sarraf a écrit :
>> 
>>         if lock is None or lock != 1:
>>             self.DispLock = False
>>         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 ?
> 
> what does "print os.name" yields ?

On Windows: nt
On Linux: posix

> 
>> 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.
>  >
> 
> Then pass the module to the initializer. Python's modules are objects...
>

Yes, that's an option. But is that the only one?
To me it looks like an ugly way of doing.
 
Ritesh
-- 
If possible, Please CC me when replying. I'm not subscribed to the list.




More information about the Python-list mailing list