Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!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; '(although': 0.05; 'parameter': 0.05; 'pointer': 0.05; 'variable,': 0.07; 'python': 0.08; 'header:In-reply-to:1': 0.09; 'object.': 0.09; 'parameter.': 0.09; 'subject:parameters': 0.09; 'ugly': 0.09; 'def': 0.12; 'wrote:': 0.14; 'local.': 0.16; 'rohdewald': 0.16; 'subject:function': 0.16; 'wolfgang': 0.16; 'workaround': 0.16; 'cc:addr:python-list': 0.17; 'variable': 0.21; 'seems': 0.21; 'cc:2**0': 0.22; 'once.': 0.23; "doesn't": 0.25; 'certainly': 0.25; 'function': 0.25; 'object': 0.26; 'pass': 0.27; '(the': 0.28; 'variables': 0.29; 'cc:addr:python.org': 0.30; 'consequence': 0.30; 'it.': 0.31; 'list': 0.33; 'actually': 0.33; 'list.': 0.33; 'thank': 0.35; 'there': 0.35; 'assignment': 0.35; 'function.': 0.35; 'received:24': 0.35; 'reference': 0.35; 'received:ca': 0.36; 'considered': 0.36; 'hold': 0.36; 'charset :us-ascii': 0.36; 'assigned': 0.37; 'references': 0.37; 'think': 0.38; 'could': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'called': 0.39; 'unless': 0.39; 'itself.': 0.39; 'either': 0.39; 'more': 0.60; 'within': 0.60; 'exact': 0.65; 'works,': 0.68; 'received:videotron.ca': 0.84 MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain; charset=us-ascii Subject: Re: scope of function parameters From: Henry Olders In-reply-to: <201105291147.26545.wolfgang@rohdewald.de> Date: Sun, 29 May 2011 16:19:11 -0400 References: <201105291147.26545.wolfgang@rohdewald.de> To: "wolfgang@rohdewald.de" X-Mailer: Apple Mail (2.1084) Cc: "python-list@python.org" 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: 40 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306700356 news.xs4all.nl 49176 [::ffff:82.94.164.166]:51545 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6569 Henry On 2011-05-29, at 5:47 , Wolfgang Rohdewald wrote: > On Sonntag 29 Mai 2011, Henry Olders wrote: >> It seems that in Python, a variable inside a function is >> global unless it's assigned. > > no, they are local > >> 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. > > it is local. But consider what you actually passed: > You did not pass a copy of the list but the list itself. > You could also say you passed a reference to the list. > All python variables only hold a pointer (the id) to > an object. This object has a reference count and is > automatically deleted when there are no more references > to it. > > If you want a local copy of the list you can either > do what you called being ugly or do just that within > the function itself - which I think is cleaner and > only required once. > > def fnc2(c): > c = c[:] > c[1] = 'having' > return c Thank you, Wolfgang. That certainly works, but to me it is still a workaround to deal with the consequence of a particular decision. From my perspective, a function parameter should be considered as having been assigned (although the exact assignment will not be known until runtime), and as an assigned variable, it should be considered local. Henry