very simple Genetic Algorithm completed

Paul McGuire ptmcg at austin.rr.com
Fri Feb 1 10:33:29 EST 2008


On Feb 1, 9:11 am, Matthew_WAR... at bnpparibas.com wrote:
>
> > 0. Tack this bit onto the end of quickga.py, and you wont have to
> > write a separate routine to import quickga and invoke evolve():
>
> >     if __name__ == "__main__":
> >         evolve()
>
> I hear you, but something I dont tend to do as I use pythons interactive
> prompt a lot, and I like things not autorunning when I import them. I
> normally add it in at the end of writing something.
>

Not to fear!  The test 'if __name__ == "__main__":' guards against
running this code when the module is imported.  The following code is
run *only* if the module is invoked directly, as in "python
quickga.py".

This way, test or demonstration code can be included right with your
library module, and it wont get run when the module is imported.  It
really is a good idiom to learn, whether you use the interactive
prompt a lot or not.

-- Paul



More information about the Python-list mailing list