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


Groups > comp.lang.python > #16406

Re: Total newbie question: Best practice

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'interpreter': 0.05; 'main()': 0.05; 'next,': 0.05; 'function,': 0.07; 'imports': 0.07; 'names.': 0.07; 'script,': 0.07; 'typed': 0.07; 'python': 0.08; 'scripts': 0.09; '__name__': 0.09; 'compiler.': 0.09; 'definitions.': 0.09; 'libraries.': 0.09; 'variables,': 0.09; 'library': 0.13; 'argument': 0.15; 'feedback.': 0.15; '"__main__":': 0.16; '(do': 0.16; 'answer,': 0.16; 'born,': 0.16; 'complexity,': 0.16; 'contexts,': 0.16; 'gradually': 0.16; 'habit.': 0.16; 'intended.': 0.16; 'narrow': 0.16; 'practice?': 0.16; 'program."': 0.16; 'received:192.168.1.104': 0.16; 'reusable': 0.16; 'uppercase': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.17; 'language': 0.17; 'wrote:': 0.18; 'arguments': 0.18; 'functions,': 0.18; 'remind': 0.18; 'yet.': 0.18; 'file,': 0.19; 'slightly': 0.19; '(which': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.20; 'maybe': 0.21; 'stuff': 0.22; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'programs.': 0.23; 'versions': 0.23; 'pieces': 0.23; 'referring': 0.23; 'similarly': 0.23; 'cc:2**0': 0.24; 'command': 0.24; 'libraries': 0.24; 'times,': 0.24; 'compiled': 0.25; 'code': 0.25; 'work,': 0.26; "i'm": 0.26; 'function': 0.27; 'import': 0.27; 'code,': 0.27; 'somebody': 0.28; 'separate': 0.28; 'exercise': 0.28; 'mine': 0.28; 'work.': 0.28; 'script': 0.28; 'pass': 0.29; 'module.': 0.29; 'script.': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'lines': 0.30; '"real': 0.30; 'closer': 0.30; 'invoke': 0.30; 'modules,': 0.30; 'operation.': 0.30; 'quickly.': 0.30; 'usually': 0.31; 'thanks': 0.31; 'shared': 0.31; 'source': 0.31; 'quite': 0.32; 'does': 0.32; 'hi,': 0.32; 'point,': 0.32; 'changes': 0.32; 'actual': 0.32; 'break': 0.32; 'comment': 0.32; 'pretty': 0.32; "isn't": 0.33; 'header:User-Agent:1': 0.33; 'rather': 0.33; 'there': 0.33; 'decide': 0.33; 'done': 0.34; 'but,': 0.34; 'file.': 0.34; 'describing': 0.34; 'end.': 0.34; 'function.': 0.34; 'yet,': 0.34; 'here,': 0.35; 'list.': 0.35; 'something': 0.35; 'moving': 0.35; 'especially': 0.35; 'external': 0.35; 'however,': 0.36; 'file': 0.36; 'starting': 0.36; 'beginning': 0.36; 'example,': 0.37; 'none': 0.37; 'own,': 0.37; 'two': 0.37; 'but': 0.37; 'run': 0.37; 'received:192': 0.37; 'problems': 0.37; 'home,': 0.63; 'you.': 0.63; 'results': 0.63; 'bear': 0.64; 'reply': 0.65; 'piece': 0.66; 'adapt': 0.67; 'hopefully,': 0.67; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'have.': 0.77; '71.': 0.84; 'complex,': 0.84; 'complexity.': 0.84; 'lengthy': 0.84; 'programming?': 0.84; 'theirs.': 0.84; 'timings': 0.84; 'few.': 0.93; 'subject:Best': 0.93
Date Tue, 29 Nov 2011 16:57:18 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15
MIME-Version 1.0
To Colin Higwell <colinh@somewhere.invalid>
Subject Re: Total newbie question: Best practice
References <jb3e06$37h$1@dont-email.me>
In-Reply-To <jb3e06$37h$1@dont-email.me>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:JtCrYH7Wk/YHChICR6zSAOjRTXEu5l5V0qwbdS4RNVz 46W9nnsv0YjMok8L5nMpXejuYAaZ60lnfKFjjPii1XJ1AaMaU4 yudKsR9Ml+iqx7tOV+SziEbOZIYRsg/chvJOj9m6V6Jfyl7q5p iYL/80AvsHSkOr5nGqLfFNrrp5VSIgTRcy6uY6fXvApGPuUbxt L6peGnMSnaqfS08K60B0waHDnWD5aXHCzV2wsZzi4WR41kzwPy E4/ZKUCNlhpBsbo72iaWca6NNlqWKZ+XRGQ+zuOoVYXrexFMlN pLZjhtFy0re9m9f4VtKGJ62vOPSGTJqUo1mJ+XB3T/LoIDhcQ= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3136.1322603871.27778.python-list@python.org> (permalink)
Lines 111
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1322603871 news.xs4all.nl 6981 [2001:888:2000:d::a6]:35278
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16406

Show key headers only | View raw


On 11/29/2011 03:06 PM, Colin Higwell wrote:
> Hi,
>
> I am just starting to learn Python (I have been at it only a few hours),
> so please bear with me. I have a few very small scripts (do you call them
> scripts or programs?) which work properly, and produce the results
> intended.
>
> However, they are monolithic in nature; i.e. they begin at the beginning
> and finish at the end. Having done a little reading, I note that it seems
> to be quite common to have a function main() at the start (which in turn
> calls other functions as appropriate), and then to call main() to do the
> work.
>
> Is that standard best practice?
>
> Thanks
>
Welcome to Python, and to the comp.lang.python list. Is this your first 
experience programming?

Yes, factoring your code from "monolithic" to "modular' (several 
functions, or even functions and classes), is good practice.

That's not to say that some problems don't deserve a monolithic answer, 
but if you're a beginner, i'd like to see you get into a modular habit.

You can use the words script and program pretty much interchangeably.  
in some contexts, each has additional connotations.  For example, 
somebody used to a compiled language may refer to a python source file 
as a script, implying it's not as sophisticated as his own product.  
But, closer to home, we usually refer to the file you directly pass to 
the interpreter as a script, and any added files that get imported, as 
modules, or libraries.

Other times, people will refer to a simple program as a script, implying 
that all it does is invoke some standard library functions, or even run 
some external programs.  But when it gets more complex, it gradually 
turns into a  "real program."


Why break up a monolith?

Several reasons.  If you factor the code into independent functions, and 
give them good names, then each piece of the program is easier to 
understand.  You will especially appreciate that if you come back to the 
code after doing something else for two weeks.  Similarly if somebody 
else has to take over your code, or maybe adapt it to a slightly 
different purpose.

Next, if it doesn't quite work, you can exercise the individual pieces 
independently, and narrow down the problem more quickly.

Next, if the progfram is slow, usually you can narrow it down to a few 
key functions that take most of the time.  You can write two versions of 
the same function, and do some careful timings to decide which one to use.

Next, some of those functions may be useful in the next program you 
write.  If you "reuse" the code by copy & paste, and find a problem in 
the new one, it's quite possible that the same problem may exist in your 
first program.  it's easier to bring those changes back if they're in a 
function than if they're in lines 14 through 71.

Finally, some of that reusable code may be worth moving to an external 
file, called a module.  Then the same copy can be literally shared 
between multiple projects.  This is how libraries were born, and you can 
write your own, eventually.

there are many other reasons, but some of them might not make sense to 
you yet.


How do you break it up?

First, separate the classic parts that most scripts will have.  Put the 
imports at the top, along with a comment describing the whole progfram's 
purpose.  Next put the global variables, which should be few.  If there 
are any constants, use all UPPERCASE for their names.

Next, put the function and class definitions.  Notice that none of them 
will be called yet, so the order of execution isn't important to the 
compiler.  Each function needs a name, and you should use a name that 
makes sense to you.  Try to write functions that work at a single level 
of complexity, and do one complete operation.  Try not to do 
input/output in the same functions that do the computation.

And finally, put the actual mainline.  It could be as simple as a call 
to main(), but it may make more sense to you to put the calls to 
argument processing here, rather than in a main function.  By arguments 
here, i'm referring to the stuff you typed on the command line when youi 
invoked the script.  This part of the code is where you do the magic 
incantation:

if __name__ == "__main__":
     main()



When the number of functions gets unwieldy, it's time to move some of 
them to a new file.  They should be related, and should work at the same 
level of complexity.  And the file name should remind you of their 
purpose.  At that point, you add an import of that file to your main 
source script.  Congratulations, you've created a module.

One catch with writing a lengthy reply is that others have already given 
you good feedback.  Hopefully, mine will complement theirs.

-- 

DaveA

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


Thread

Total newbie question: Best practice Colin Higwell <colinh@somewhere.invalid> - 2011-11-29 20:06 +0000
  Re: Total newbie question: Best practice Arnaud Delobelle <arnodel@gmail.com> - 2011-11-29 20:34 +0000
    Re: Total newbie question: Best practice Neil Cerutti <neilc@norwich.edu> - 2011-11-29 21:12 +0000
  Re: Total newbie question: Best practice Chris Angelico <rosuav@gmail.com> - 2011-11-30 07:36 +1100
  Re: Total newbie question: Best practice Dave Angel <d@davea.name> - 2011-11-29 16:57 -0500
    Re: Total newbie question: Best practice Colin Higwell <colinh@somewhere.invalid> - 2011-11-29 23:08 +0000
  RE: Total newbie question: Best practice Shambhu Rajak <Shambhu.Rajak@kpitcummins.com> - 2011-11-30 08:50 +0000
  Re: Total newbie question: Best practice "Pedro Henrique G. Souto" <pedro.h.souto@gmail.com> - 2011-11-30 09:36 -0200

csiph-web