Module imports during object instantiation

Ritesh Raj Sarraf rrs at researchut.com
Wed Aug 15 15:43:18 EDT 2007


On Aug 16, 12:16 am, Ritesh Raj Sarraf <r... at researchut.com> wrote:
> On Aug 15, 11:42 pm, Neil Cerutti <horp... at yahoo.com> wrote:
>
> > On 2007-08-15, Ritesh Raj Sarraf <r... at researchut.com> wrote:
> > > Or am I terribly missing something that you are trying to tell ?
>
> > I didn't see log = Log() in your example. Sorry for the
> > excursion.
>
> > Are you sure os.name is 'posix' on your system?
>
> Here again for you:
>
> In [6]: cat rick.py
> IPython system call: cat rick.py
> import os, sys
>
> class Log:
>     def __init__(self, verbose, lock = None):
>         self.VERBOSE = bool(verbose)
>         self.lock = bool(lock)
>
>         if self.lock:
>             self.dispLock = threading.Lock()
>         else:
>             self.dispLock = None
>
>         if os.name == 'posix':
>             try:
>                 import foobar
>             except ImportError, e:
>                 print >> sys.stderr, e
>
>             self.platform = 'posix'
>             self.color = get_colors()
>
>         elif os.name in ['nt', 'dos']:
>             self.platform = 'microsoft'
>             try:
>                 import SomeModule
>             except ImportError, e:
>                 # comment out next line before going to prod...
>                 print >> sys.stderr, e
>
>                 self.color = None
>             else:
>                 self.color = SomeModule.get_colors_windows()
>         else:
>             self.platform = None
>             self.color = None
>
> In [7]: from rick import Log
>
> In [8]: log = Log(verbose = True)
> No module named foobar
> ---------------------------------------------------------------------------
> exceptions.NameError                                 Traceback (most
> recent call last)
>
> /tmp/<ipython console>
>
> /tmp/rick.py in __init__(self, verbose, lock)
>      18
>      19             self.platform = 'posix'
> ---> 20             self.color = get_colors()
>      21
>      22         elif os.name in ['nt', 'dos']:
>
> NameError: global name 'get_colors' is not defined
>

Oops!!! Looks like I completely missed this. It _did_ print the error
message.
Apologies to all for not keeping a close eye on the error message.




More information about the Python-list mailing list