Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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; 'parameter': 0.05; 'snippet': 0.07; 'declarations': 0.09; 'parameter.': 0.09; 'subject:parameters': 0.09; 'output': 0.11; 'def': 0.12; 'debugging': 0.14; "['a": 0.16; 'adopted': 0.16; 'function?': 0.16; 'illustrates': 0.16; 'made-up': 0.16; 'main():': 0.16; 'subject:function': 0.16; 'ugly.': 0.16; 'workaround': 0.16; 'variable': 0.21; 'seems': 0.21; 'here?': 0.23; 'code': 0.24; "doesn't": 0.25; 'function': 0.25; 'changed': 0.25; 'statement': 0.26; 'pass': 0.27; 'changing': 0.28; 'problem': 0.28; 'print': 0.31; 'to:addr:python-list': 0.33; 'rule': 0.34; 'there': 0.35; 'function.': 0.35; 'received:24': 0.35; 'received:ca': 0.36; 'considered': 0.36; 'charset:us-ascii': 0.36; 'something': 0.37; 'but': 0.38; 'third': 0.38; 'should': 0.39; 'unless': 0.39; 'spent': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'missing': 0.40; 'below': 0.61; 'order': 0.62; 'received:videotron.ca': 0.84 MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain; charset=us-ascii From: Henry Olders Subject: scope of function parameters Date: Sun, 29 May 2011 04:30:52 -0400 To: python-list@python.org X-Mailer: Apple Mail (2.1084) 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: 82.94.164.166 X-Trace: 1306661462 news.xs4all.nl 49046 [::ffff:82.94.164.166]:33531 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6521 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. It doesn't make sense to me to pass a global to a function as a parameter. One workaround is to call a function with a copy of the list, eg in fnc1 I would have the statement "return fnc2(b[:]". But this seems ugly. Are there others who feel as I do that a function parameter should always be local to the function? Or am I missing something here? Henry