Newbie Question regarding __init__()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Aug 1 06:48:50 CEST 2009


En Sat, 01 Aug 2009 00:13:05 -0300, Nat Williams <nat.williams at gmail.com>  
escribió:

> One other thing.  I'm a little confused by the first line of
> dcObject.__init__:
>
> self.init_Pre() and self.init_Exec()
>
> I suspect this does not do what you think it does.  init_Pre and  
> init_Exec
> will both be called by this expression (unless init_Pre throws an  
> exception,
> of course).  You're not getting anything here that you wouldn't by just
> calling each method on a separate line, except just making it harder to
> read.

Well, perhaps Simon didn't grasp Python's syntax and semantics very well  
yet, but his code would work as designed after fixing obvious errors:

class dcObject(object):
     """Base Object"""
     def __init__(self):
         """default init method has the form
         of init_Pre() and init_Exec
         init_Post()"""
         self.init_Pre() and self.init_Exec()
         self.init_Post()

     def init_Pre(self):
         """Always called before init_Exec()
         if it returns false init_Exec is
         not executed"""
         return True

     def init_Exec(self):
         """Base __init__ code goes here and this is
         only executed if init_Pre() returns true"""
         return True

     def init_Post(self):
         """Always called after the init_Pre() and
         init_Exec()"""
         return True

init_Pre might return False, in that case init_Exec would not be executed.  
init_Post is always called. And that's exactly what the docstrings say.

I would use an `if` statement instead:
if self.init_Pre(): self.init_Exec()
and make init_Exec and init_Post not return anything if they have no  
intrinsic meaning. And probably use better names. But basically the  
structure is OK.

-- 
Gabriel Genellina




More information about the Python-list mailing list