Module imports during object instantiation
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Aug 13 03:59:07 EDT 2007
Ritesh Raj Sarraf a écrit :
> 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:
Don't use an identity test here. There are quite a lot of values that
eval to either True or False in a boolean context without being the True
or False objects.
> self.VERBOSE = True
> else: self.VERBOSE = False
Also, this test is rather dumb - in fact you just don't need any test:
self.VERBOSE = bool(verbose)
>
> 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 ?
> 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...
More information about the Python-list
mailing list