[Tutor] What not to put in __init__()

Kent Johnson kent37 at tds.net
Wed Oct 4 04:26:08 CEST 2006

Tony Cappellini wrote:
> I've just inherited a lot of python code to maintain.
> The __init__ functions in many of the classes are very long- some over 
> 100 lines.
> I like to keep functions/methods short & readable at a glance, if possible.
> 1. Is it good methodology to move some code from _init__ to it's own 
> method within the class?

Sure. __init__() is just another method, there's no reason not to break 
it up if that will make it more readable. You might prefix the names of 
the new functions with _ to indicate that they are implementation 
details, not part of the public interface.

I once worked on a Java class that had a 1000-line constructor, so 
consider yourself lucky :-)
>  For example there are many for loops with several levles of nested if 
> statements within them.
> I'd like to move each of these for/if chunks to distinct methods, so to 
> simplify the look of __init__, without modifying how it works.

Go for it. You might want to write some unit tests first if you don't 
have any.


More information about the Tutor mailing list