Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'chunk': 0.07; 'received:verizon.net': 0.07; 'subject:when': 0.07; 'terry': 0.07; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:python': 0.11; 'am,': 0.12; 'def': 0.15; '"""find': 0.16; 'chunks': 0.16; 'lookup': 0.16; 'reedy': 0.16; 'subject:function': 0.16; 'subject:writing': 0.16; 'unpacking': 0.16; 'wrote:': 0.16; 'jan': 0.19; 'header:In- Reply-To:1': 0.22; "i'm": 0.27; 'work.': 0.27; '(see': 0.28; 'comfortable': 0.28; 'matches': 0.29; 'example': 0.30; 'probably': 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'assignment': 0.34; 'header:X-Complaints-To:1': 0.35; 'none': 0.37; 'sequence': 0.37; 'perform': 0.37; 'received:org': 0.38; 'menu': 0.38; 'steven': 0.38; 'subject:: ': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'lower': 0.64; 'subject:any': 0.84; 'subject:there': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: is there any principle when writing python function Date: Tue, 23 Aug 2011 14:29:38 -0400 References: <4e53c5ab$0$29996$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <4e53c5ab$0$29996$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314124224 news.xs4all.nl 23843 [2001:888:2000:d::a6]:36051 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12111 On 8/23/2011 11:22 AM, Steven D'Aprano wrote: > Even 7=C2=B12 is probably excessive: I find that I'm most comfortable w= ith > functions that perform 4=C2=B11 chunks of work. An example from one of = my > classes: > > def find(self, prefix): > """Find the item that matches prefix.""" > prefix =3D prefix.lower() # Chunk #1 > menu =3D 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 assignment enumerate sequence unpacking for-looping if-conditioning lower startswith return That is 9, which is enough. --=20 Terry Jan Reedy