I haven't seen a recursive version, yet, so here.

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Feb 20 04:45:13 EST 2002


caljason76 at yahoo.com (Jason) wrote in 
news:e9e6dd1f.0202191541.30eb9f62 at posting.google.com:

> # I am a Python novice so I don't really understand how things
> # work, yet.  So some of this may either be wrong or not the
> # Python way.  Also I have no idea why the 'if __name__ == ...'
> # construct is needed instead of just the naked main statements.

There are two ways to run the code you have written. The way you probably 
expect is that someone just runs the script, but they might decide that 
some of your classes or functions are useful in another program. If so, 
they could import your code as a module.
If your code is imported, then the lines protected by 'if __name__==...' 
will not be executed. If you just put these lines straight into the file 
then anyone trying to import it will get the whole script running instead 
of being able to pull out a few classes or functions.

So the answer to you question is that this is purely a convention, you dont 
have to protect the main code in a script, but if you follow the convention 
you will find you dont have to keep rewriting the same functions as you can 
reuse the originals.

> # Also, can somebody help me find the documentation to the builtin
> # procedures, like print().  I checked the __builting__ module docs
> # but there is nothing there.  Thanks
> #
1. print is a statement, not a builtin procedure.
2. Run Python in interactive mode, type "help()" (without the quote marks) 
and you will get a prompt that looks like "help>" type "print" at this 
prompt and you will get the documentation for the print statement.
3. Alternatively look in the Language Reference section 6.6



-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list