Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python,': 0.01; 'languages,': 0.03; 'lines.': 0.05; 'memory.': 0.05; 'function,': 0.07; 'indeed,': 0.07; 'received:verizon.net': 0.07; 'subject:when': 0.07; 'terry': 0.07; 'python': 0.08; 'exceptions': 0.09; 'invocation': 0.09; 'omit': 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; 'block,': 0.16; 'bytecode': 0.16; 'constructs': 0.16; 'defs': 0.16; 'function?': 0.16; 'iteration.': 0.16; 'reedy': 0.16; 'subject:function': 0.16; 'subject:writing': 0.16; 'versus': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'avoiding': 0.18; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'faster,': 0.23; 'function': 0.27; '(and': 0.29; 'print': 0.29; 'lines': 0.30; 'recursion': 0.30; 'functional': 0.31; 'translate': 0.31; 'least': 0.31; 'does': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'header :User-Agent:1': 0.34; 'things': 0.34; 'retain': 0.34; 'header:X -Complaints-To:1': 0.35; 'unless': 0.36; 'example,': 0.37; 'languages': 0.37; 'but': 0.37; 'received:org': 0.38; 'some': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'enough': 0.39; 'header :Mime-Version:1': 0.39; 'define': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; '100': 0.73; 'alternative.': 0.84; 'habit': 0.84; 'purpose,': 0.84; 'so:': 0.84; 'subject:any': 0.84; 'subject:there': 0.91; 'complexity': 0.93; 'many,': 0.93 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:19:15 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314123599 news.xs4all.nl 23889 [2001:888:2000:d::a6]:45834 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12110 On 8/23/2011 7:59 AM, smith jack wrote: > i have heard that function invocation in python is expensive, That comes into play when chosing between list2 = map(lambda x: 2*x, list1) # versus list2 = [2*x for x in list1] It also comes into play when choosing between looping with recursion (function calls) versus looping with iteration (while/for). In Python, the iteration is faster, while some functional languages omit looping syntax constructs and perhaps auto-translate some recursion to iteration. > but makelots of functions are a good design habit in many other languages, Same for Python, with the exceptions noted above of avoiding trivial one-use functions when there is an alternative. > is there any principle when writing python function? Same as usual. Functions define new words and create new abstractions than encapsulate a unit of computation. > for example, how many lines should form a function? 1 to many, as long as the 1 is more complex than 2*x, unless the trivial function is required for a callback. I doubt the stdlib has many defs longer than 100 lines. Try the following: complex enough that the function call overhead does not matter; simple enough to be understood as a unit. I just came up with the following hypothesis: the complexity of a function is related to the number of *different* functions used to define it: x = a*b + c/d - e**f is more complex (harder to understand) than x = a + b + c + d + e + f For this purpose, different statememts count as functions (and indeed, they translate to bytecode functions. So: for i in iterable: if f(i): print i is more complex than a = 1 b = 2 c = 3 d = 4 People can retain at most about 10 different things in short term memory. So perhaps 10 different 'functions' within a function, or at least a commented block, is enough. -- Terry Jan Reedy