Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.02; '(at': 0.04; 'static': 0.04; 'subsequent': 0.05; 'class,': 0.07; 'defaults': 0.07; 'discard': 0.07; 'variables': 0.07; 'attributes': 0.09; 'correct,': 0.09; 'function,': 0.09; 'namespace': 0.09; 'obsolete': 0.09; 'php,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stack.': 0.09; 'to)': 0.09; 'variables.': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'stored': 0.12; 'attributes,': 0.16; 'before.': 0.16; 'bind': 0.16; 'called.': 0.16; 'caveat': 0.16; 'compiler.': 0.16; "function's": 0.16; 'languages)': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:programming': 0.16; 'subject:question.': 0.16; 'though)': 0.16; 'wrote:': 0.18; '(not': 0.18; 'variable': 0.18; 'stack': 0.19; 'written': 0.21; '>>>': 0.22; 'appears': 0.22; 'programming': 0.22; 'proposed': 0.22; 'header:User- Agent:1': 0.23; 'fairly': 0.24; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'appear': 0.29; 'specifically': 0.29; 'chris': 0.29; '(like': 0.30; 'gives': 0.31; "skip:' 10": 0.31; 'too.': 0.31; 'usually': 0.31; '>>>>': 0.31; 'commonly': 0.31; 'values.': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'call.': 0.33; 'implemented': 0.33; 'comment': 0.34; 'common': 0.35; 'except': 0.35; 'possible.': 0.35; 'something': 0.35; 'definition': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'accessible': 0.36; 'functions.': 0.36; 'object,': 0.36; 'similar': 0.36; 'example,': 0.37; 'two': 0.37; 'easily': 0.37; 'being': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'heard': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'then,': 0.60; 'most': 0.60; 'new': 0.61; 'matter': 0.61; 'first': 0.61; 'times': 0.62; 'name': 0.63; 'such': 0.63; 'teaching': 0.64; 'more': 0.64; 'thomas': 0.65; 'computers': 0.72; 'paper': 0.75; 'directed': 0.83; 'received:fios.verizon.net': 0.84; 'closer.': 0.91; 'sheet': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Generarl programming question. Date: Sat, 11 Apr 2015 17:12:14 -0400 References: <12030326.cc9aoE7jz1@PointedEars.de> <3458425.12qjem4LOE@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <3458425.12qjem4LOE@PointedEars.de> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 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: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428786783 news.xs4all.nl 2841 [2001:888:2000:d::a6]:47421 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 7248 X-Received-Body-CRC: 3678737570 Xref: csiph.com comp.lang.python:88842 On 4/11/2015 3:19 PM, Thomas 'PointedEars' Lahn wrote: > Terry Reedy wrote: > >> On 4/11/2015 12:23 PM, Thomas 'PointedEars' Lahn wrote: >>> Chris Angelico wrote: >>>> The 'x' inside each function is completely separate, no matter how >>>> many times they get called. They're usually stored on something call= ed >>>> a "call stack" - you put another sheet of paper on top of the stack >>>> every time you call a function, local variables are all written on >>>> that paper, and when you return from a function, you discard the top= >>>> sheet and see what's underneath. >>> >>> Thank you for that description; I shall use it from now on when teach= ing >>> laymen about the call stack. >> >> What Chris is describing is one local namespace (sheet of paper) per >> function *call*. > I *know* what he is describing: the *call* stack. My comment above was directed not at you specifically but at the OP,=20 Jonas, who appears to have had a mental model (like the following) in=20 which recursion is not possible. I think this mental model is fairly=20 common among programming newbies. And it is not crazy, just obsolete=20 and superceded. And, we constantly talk about a function's local names, = which is correct, without constantly adding the caveat that in Python=20 (and most modern languages) they are instanced per call. I think of functions as being something like a class, in that each call=20 gives a new instance with a new set of named values. >> In early Fortran (at least the first version I used), >> there was one local namespace (sheet) per *function*. > > The names in such namespaces are now called static variables. AFAIK, P= ython > does not have them, but PHP, for example, has: > > function foo () > { > static $bar =3D 1; > $bar *=3D 2; > return $bar; > } > > The variable $bar then keeps its last value for subsequent calls of foo= (). In Python, one can do something similar with attributes, except that=20 attributes are easily accessible from outside the function. Mutable=20 defaults probably come closer. def doubler(_val=3D[1])" _val[0] *=3D 2 return _val[0] print(doubler(), doubler(), doubler()) # 2 4 8 >> The call stack was a stack of (pointers to) functions. > > It would appear that the commonly used definition of =E2=80=9Ccall stac= k=E2=80=9D has > considerably changed since then, since I have been programming computer= s for > more than two decades now (not including FORTRAN, though) and never hea= rd of > your definition before. I don't know what the stack required for returns was called in Fortran=20 or how it was implemented in any particular compiler. >> It has been proposed that Python use a hybrid model. Function objects= > > Interesting. I did not know that functions are objects in Python, too.= In Python, everything you can bind a name to is an object, and in 3.x,=20 an instance of the base class 'object'. --=20 Terry Jan Reedy