[Tutor] Reviewing scripts & main statements in function

Daniel Ehrenberg littledanehren at yahoo.com
Thu Jan 15 17:33:32 EST 2004


Barnaby Scott wrote:
> Thanks very much for that. I was interested to see
> your comments, and am as pleased to receive
> stylistic
> help as much as any other kind.
> 
> Can I ask one other question, which may have wider
> relevance for others?:
> 
> You (or another contributor) put the main statement
> block into a function and then called it with the
> line
> 
> if __name__ == '__main__': main()
> 
> I have seen this before - is it universally good
> practice? What about scripts that were only ever
> intended to be run standalone?

Yes, this is considered universally good practice.
Even if this was originally only intended to be run
standalone, there is still a chance that someone will
want to reuse it without having to copy some of the
source code into a new file by using it as a module.
Your code was very well-structured and reusable, apart
from that one thing that I had to put in a function.
> 
> It also makes me think - if this is good practice,
> should you always be prepared for someone to import
> your script as a module, and maybe even subclass
> some
> of my objects etc? If they did, they would get a
> rude
> shock, as the classes are not very 'self-contained'.
> For example they make reference to each other and to
> global variables in the module. Should classes
> always
> have every piece of data they will need passed to
> them
> at the creation of an instance, or in other ways
> make
> themselves 'available' more universally?
> 
As long as the classes are not coppied and pasted into
another file, it will work fine. There is no real
reason to demand that everything be self-contained in
Python (although other languages have restrictions
which make it like that).

> Thanks again - I really do appreciate all these
> opportunites to learn more.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus



More information about the Tutor mailing list