Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: When I need classes? Date: Mon, 11 Jan 2016 13:57:41 +1100 Lines: 29 Message-ID: References: <56927291.6010009@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de DgjuW7Ea4y9bxJeZSfDNFwk9xH/67nhHC9ft2PO50Vhg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'needed,': 0.05; '(b)': 0.07; '__name__': 0.07; 'classes.': 0.07; 'main()': 0.07; 'logic': 0.09; 'def': 0.13; 'variables': 0.15; "'__main__':": 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'wrote:': 0.16; 'later': 0.16; 'subject:need': 0.18; '(a)': 0.22; 'aspect': 0.22; 'cheers,': 0.22; 'defined': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'script': 0.25; 'header:User- Agent:1': 0.26; 'behaviour': 0.29; 'putting': 0.30; 'another': 0.32; "can't": 0.32; 'michael': 0.33; 'avoiding': 0.33; "i'll": 0.33; 'structure': 0.34; 'but': 0.36; 'development.': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'received:localdomain': 0.38; 'speak': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'easy': 0.60; 'introduction': 0.63; 'cameron': 0.66; 'here': 0.66; 'as:': 0.79; '>if': 0.84; 'subject:When': 0.84 Content-Disposition: inline In-Reply-To: <56927291.6010009@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101456 On 10Jan2016 08:02, Michael Torrie wrote: >I can't speak to Flask or any other web development. But for simple >scripts, I'll start out with no classes. Just functions as needed, and >some main logic. I always use the idiom: > >if __name__ == '__main__': > # do main logic here > >This way I can import functions defined in this script into another >script later if I want. I always structure this aspect as: ... at or near top of script ... def main(argv): ... do main logic here ... ... at bottom ... if __name__ == '__main__': sys.exit(main(sys.argv)) This has the benefits of (a) putting the main program at the top where it is easy to see/find and (b) avoiding accidently introduction of dependence on global variables - because verything is inside main() it has the same behaviour as any other function. Cheers, Cameron Simpson