[Tutor] Python best practices

Alan Gauld alan.gauld at btinternet.com
Mon Nov 30 01:57:23 CET 2009


"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. 




More information about the Tutor mailing list