Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101456 > unrolled thread
| Started by | Cameron Simpson <cs@zip.com.au> |
|---|---|
| First post | 2016-01-11 13:57 +1100 |
| Last post | 2016-01-11 07:31 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2016-01-11 13:57 +1100 |
| Subject | Re: When I need classes? |
| Message-ID | <mailman.1.1452481599.13488.python-list@python.org> |
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>
[toc] | [next] | [standalone]
| From | Ulli Horlacher <framstag@rus.uni-stuttgart.de> |
|---|---|
| Date | 2016-01-11 07:31 +0000 |
| Message-ID | <n6vlp7$69a$1@news2.informatik.uni-stuttgart.de> |
| In reply to | #101456 |
Cameron Simpson <cs@zip.com.au> wrote: > 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)) I, as a Python beginner, came to the same solution! It seems, it was a good idea :-) -- Ullrich Horlacher Server und Virtualisierung Rechenzentrum IZUS/TIK E-Mail: horlacher@tik.uni-stuttgart.de Universitaet Stuttgart Tel: ++49-711-68565868 Allmandring 30a Fax: ++49-711-682357 70550 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web