Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101456

Re: When I need classes?

From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: When I need classes?
Date 2016-01-11 13:57 +1100
Message-ID <mailman.1.1452481599.13488.python-list@python.org> (permalink)
References <56927291.6010009@gmail.com>

Show all headers | View raw


On 10Jan2016 08:02, Michael Torrie <torriem@gmail.com> 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 <cs@zip.com.au>

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: When I need classes? Cameron Simpson <cs@zip.com.au> - 2016-01-11 13:57 +1100
  Re: When I need classes? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-11 07:31 +0000

csiph-web