[Tutor] Python best practices

Albert-Jan Roskam fomcl at yahoo.com
Mon Nov 30 10:58:21 CET 2009


I'm currently reading the book "Code Complete" (I don't know the author name), which gives a lot of useful best practices. It's not specifically about one programming language. The author stresses that the majority of those practices are a matter of consensus/consistency and not a matter of religion. There is no one best way to do it.
 
Re: functions, the author recommends that they have one purpose and one purpose only, and that a function returns only one result.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 11/30/09, Alan Gauld <alan.gauld at btinternet.com> wrote:


From: Alan Gauld <alan.gauld at btinternet.com>
Subject: Re: [Tutor] Python best practices
To: tutor at python.org
Date: Monday, November 30, 2009, 1:57 AM


"spir" <denis.spir at free.fr> wrote

>> > - functions should return one value (im not 100% of this one)
>> 
>> I 100% disagree or with this one.
> 
> Could you explain this bit, Lie? I'm very interested.
> I use multiple-value result myself, for it's so practicle in given cases.

My take on this is that in Python its OK to return multiple values if it
is as a tuple - because a tuple is really a single value. Its like returning
a record in Pascal or a struct in C or a List in Lisp...

> But it makes me uneasy; also have the impression (why?) it
> reveals wrong design.

Its better than....

> a function both to have an effect (eg assignment outside the func scope) and
> return a value.

Side effects in functions are nearly always bad news and are always
avoidable if you can return multiple values (or pass arguments by reference).

> "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs function".

You can have Command/Query separation without having side-effects.
A Query is a procedure or function that doesn't change anything but just
returns a result. A Command changes something, but it can be the thing
passed to it - or if a method of a class the internal data of the class.
Again a command can be a function or a procedure. Those are separate
concepts. (I very rarely write procedures in real programs - there is nearly
always something useful you can return - and in an object that's usually
a minimum of self! (Returning self is the default idiom in Smalltalk - it
allows chaining of methods)

HTH,

Alan G. 

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/7059ec93/attachment.htm>


More information about the Tutor mailing list