is there any principle when writing python function
steve+comp.lang.python at pearwood.info
Wed Aug 24 03:44:14 CEST 2011
Terry Reedy wrote:
> On 8/23/2011 11:22 AM, Steven D'Aprano wrote:
>> Even 7±2 is probably excessive: I find that I'm most comfortable with
>> functions that perform 4±1 chunks of work. An example from one of my
>> def find(self, prefix):
>> """Find the item that matches prefix."""
>> prefix = prefix.lower() # Chunk #1
>> menu = self._cleaned_menu # Chunk #2
>> for i,s in enumerate(menu, 1): # Chunk #3
>> if s.lower().startswith(prefix):
>> return i
>> return None # Chunk #4
>> So that's three one-line chunks and one three-line chunk.
> In terms of different functions performed (see my previous post), I see
> attribute lookup
> sequence unpacking
> That is 9, which is enough.
I think we have broad agreement, but we're counting different things.
Analogy: you're counting atoms, I'm grouping atoms into molecules and
It's a little like phone numbers: it's not an accident that we normally
group phone numbers into groups of 2-4 digits:
011 23 4567 8901
In general, people can more easily memorise four chunks of four digits (give
or take) than one chunk of 13 digits: 0112345678901.
More information about the Python-list