claird at lairds.us
Sun Jan 16 17:08:07 CET 2005
In article <lqbcb2-r9l.ln1 at home.rogerbinns.com>,
Roger Binns <rogerb at rogerbinns.com> wrote:
>> runner.py:200: Function (detectMimeType) has too many returns (11)
>> The function is simply a long "else-if" clause, branching out to different
>> return statements. What's wrong? It's simply a "probably ugly code" advice?
>That is also advice. Generally you use a dict of functions, or some other
>structure to lookup what you want to do.
This interests me. Let's be more precise.
Use, it's absolutely true that good Python style exploits dicts
far more than newcomers realize (although somewhat less than Lua,
a language which intriguingly pushes dictionary pragmatics to the
limit). In particular, it is frequently the case that beginners'
appetite for a "switch" is best met by a lesson in use of a
dictionary of functions.
HOWEVER, proliferation of functions itself adds weight. My own
counsel is that a simple sequence of
often is exactly the right definition for a specific function.
I, incidentally, prefer this form over the
the original poster described.
So: yes, "[g]enerally you use a dict of functions" when PyChecker
thinks you have "too many returns", but I do NOT advise it for the
specific case that appears to be at hand.
More information about the Python-list