assignment expression peeve

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Wed Oct 15 01:23:35 EDT 2003


On 14 Oct 2003 22:19:07 -0700, Paul Rubin wrote:
> Ben Finney <bignose-hates-spam at and-benfinney-does-too.id.au> writes:
>> If you want to do lots of different things, you really should be
>> placing them in different functions:
> 
> Yeah, I mentioned I could do something like that, but that it was
> overkill for such a small list.

The example you gave was contrived, sure.  But even it was getting
complex enough that it was hard to parse visually -- and using a switch
or case structure wouldn't have made it easier.  The problem was the
fact that each case was doing something completely different.

Once each case is more complex than a smple statement or two, it makes
more sense to handle them in separate functions.  If a task expands to
several statements, the likelihood is that it will keep expanding.
Better that it do so in a separate function rather than bloating some
monstrous switch structure.

> But the if/elif/elif idiom exists because a lot of the time, it's the
> most natural and clear and correct way to write the code.

Yes, when each case is trivially simple and easy to read along with all
the others, it does make sense to keep them together.  The if/elif/else
structure works fine there.

That's not so for the example you gave, where each case was more complex
and differed sufficiently from the others that they were hard to see as
a single structure.  Thus why I recommended using separate functions if
you want to do lots of different things.

-- 
 \      "There is more to life than increasing its speed."  -- Mahatma |
  `\                                                            Gandhi |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list