Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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; 'python,': 0.01; 'attributes': 0.05; 'parameter': 0.05; 'passes': 0.05; 'function,': 0.07; 'names.': 0.07; 'prints': 0.07; 'snippet': 0.07; 'terry': 0.07; 'python': 0.08; 'declarations': 0.09; 'globals': 0.09; 'object.': 0.09; 'parameter.': 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; 'slot': 0.09; 'subject:parameters': 0.09; 'unnamed': 0.09; 'output': 0.11; 'def': 0.12; 'am,': 0.14; 'debugging': 0.14; 'wrote:': 0.14; "['a": 0.16; 'adopted': 0.16; 'container.': 0.16; 'happens.': 0.16; 'illustrates': 0.16; 'instances.': 0.16; 'made-up': 0.16; 'main():': 0.16; 'names*': 0.16; 'namespace.': 0.16; 'reedy': 0.16; 'replaces': 0.16; 'subject:function': 0.16; 'class,': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'seems': 0.21; 'right,': 0.22; 'right.': 0.22; 'module,': 0.23; 'objects': 0.23; 'code': 0.24; "doesn't": 0.25; 'function': 0.25; 'changed': 0.25; 'parameters': 0.26; 'statement': 0.26; 'string': 0.26; 'object': 0.26; 'pass': 0.27; 'example': 0.27; 'changing': 0.28; 'problem': 0.28; 'binding': 0.30; 'print': 0.31; "skip:' 10": 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr:python- list': 0.33; 'list': 0.33; 'question': 0.34; 'rule': 0.34; 'header :User-Agent:1': 0.35; 'function.': 0.35; 'considered': 0.36; 'received:org': 0.38; 'but': 0.38; 'third': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'unless': 0.39; 'spent': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'results': 0.60; 'your': 0.60; 'below': 0.61; 'order': 0.62; 'collection': 0.72; 'container': 0.73; 'identically': 0.84; 'locals': 0.84; 'handing': 0.91; 'many,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: scope of function parameters Date: Sun, 29 May 2011 16:27:17 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 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: 61 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306700850 news.xs4all.nl 49048 [::ffff:82.94.164.166]:53772 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6570 On 5/29/2011 7:59 AM, Mel wrote: > Henry Olders wrote: > >> I just spent a considerable amount of time and effort debugging a program. >> The made-up code snippet below illustrates the problem I encountered: >> >> def main(): >> a = ['a list','with','three elements'] >> print a >> print fnc1(a) >> print a >> >> def fnc1(b): >> return fnc2(b) >> >> def fnc2(c): >> c[1] = 'having' >> return c >> >> This is the output: >> ['a list', 'with', 'three elements'] >> ['a list', 'having', 'three elements'] >> ['a list', 'having', 'three elements'] >> >> I had expected the third print statement to give the same output as the >> first, but variable a had been changed by changing variable c in fnc2. >> >> It seems that in Python, a variable inside a function is global unless >> it's assigned. This rule has apparently been adopted in order to reduce >> clutter by not having to have global declarations all over the place. >> >> I would have thought that a function parameter would automatically be >> considered local to the function. Function *parameters* are names, the first *local names* of the function. >> It doesn't make sense to me to pass a global to a function as a parameter. You are right, in a way;-). Global *names* are just names. When you call a function, you pass *objects* as *arguments*. Of course, you may refer to the object by a global name to pass it, or you can pass a string object that contains a global name. > > It doesn't look like a question of local or global. fnc2 is passed a > container object and replaces item 1 in that container. You see the results > when fnc2 prints the object it knows as `c`, and you see again when main > prints the object it knows as `a`. Python doesn't pass parameters by > handing around copies that can be thought of as local or global. Python > passes parameters by binding objects to names in the callee's namespace. In > your program the list known as `a` in main is identically the same list as > the one known as `c` in fnc2, and what happens happens. Right. Python has one unnamed 'objectspace'. It has many, many namespaces: builtins, globals for each module, locals for each function and class, and attributes for some instances. Each name and each collection slot is associated with one object. Each object can have multiple associations, as in the example above. -- Terry Jan Reedy