Module imports during object instantiation

Ritesh Raj Sarraf rrs at researchut.com
Fri Aug 10 17:53:40 EDT 2007


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


        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 ?
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.


Ritesh
-- 
If possible, Please CC me when replying. I'm not subscribed to the list.




More information about the Python-list mailing list