Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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; 'parameter': 0.05; 'names.': 0.07; 'refers': 0.07; 'subject:two': 0.07; 'append': 0.09; 'modifies': 0.09; 'none.': 0.09; 'object.': 0.09; 'referenced': 0.09; 'referencing': 0.09; 'subject:parameters': 0.09; 'this:': 0.10; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; "'a'": 0.16; 'assigns': 0.16; 'model.': 0.16; 'scope.': 0.16; 'subject:function': 0.16; 'tue,': 0.17; 'object,': 0.19; 'header :In-Reply-To:1': 0.21; 'modify': 0.22; 'received:209.85.210.174': 0.23; 'received:mail-iy0-f174.google.com': 0.23; 'objects': 0.23; 'function': 0.25; 'changed': 0.25; 'object': 0.26; 'message- id:@mail.gmail.com': 0.28; 'variables': 0.29; 'second,': 0.30; 'situation,': 0.30; 'calling': 0.31; 'print': 0.31; 'to:addr :python-list': 0.33; 'list.': 0.33; 'however,': 0.34; '[1]': 0.34; 'daniel': 0.34; 'there': 0.35; 'function.': 0.35; 'reference': 0.35; 'considered': 0.36; 'several': 0.36; 'uses': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'references': 0.37; 'case': 0.37; 'docs': 0.38; 'portion': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'subject: (': 0.39; 'received:209': 0.39; 'either': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'best': 0.60; '31,': 0.65; 'explanations': 0.67; 'indicator': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=uuRZw44K+MYoyW0/ela6UzcgkHjtTvdk0b0ApNE75Hs=; b=aHlp1SLWH8cNUwXicpU6/kzODUQ6SDHij1WwPGnEBb9+rpl6zTs3AjTTsfKUDwFpFJ 8c+7zgvF3c35ydtDaw+Zgf6Wz52RbmBL2vnNxq7UfL1B/3gHsKvG/j4G/2hL70VpmUCP 9tjXFloQxEo4+1qb04NVJEgXV4T9AIsZ4xJa4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=e78Jgi3nBC2jY4m7Vn9+A7d+cX/SYadlJgeSoE5TwwxmuO1URfSnIi12vjxbzzv3Xu BC8m74jJjLu0iOAvul89K2sioTfSYbTMvfTmmah21QsfxPmvXmUoo1BH3H8OM7FPrr1B BYOUti0zHpnrlux02zM9kEjf86r7wuMeqLdbU= MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 31 May 2011 12:11:17 +1100 Subject: Re: scope of function parameters (take two) From: Daniel Kluev To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 46 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306804280 news.xs4all.nl 49042 [::ffff:82.94.164.166]:43829 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6687 On Tue, May 31, 2011 at 11:28 AM, Henry Olders wrote: > What I would like is that the variables which are included in the function definition's parameter list, would be always treated as local to that function You still mis-reading docs and explanations you received from the list. Let me try again. First, there are objects and names. Calling either of them as 'variables' is leading to this mis-understanding. Name refers to some object. Object may be referenced by several names or none. Second, when you declare function `def somefunc(a, b='c')` a and b are both local to this function. Even if there are some global a and b, they are 'masked' in somefunc scope. Docs portion you cited refer to other situation, when there is no clear indicator of 'locality', like this: def somefunc(): print a In this - and only in this - case a is considered global. Third, when you do function call like somefunc(obj1, obj2) it uses call-by-sharing model. It means that it assigns exactly same object, that was referenced by obj1, to name a. So both obj1 and _local_ a reference same object. Therefore when you modify this object, you can see that obj1 and a both changed (because, in fact, obj1 and a are just PyObject*, and point to exactly same thing, which changed). However, if you re-assign local a or global obj1 to other object, other name will keep referencing old object: obj1 = [] def somefunc(a): a.append(1) # 'a' references to the list, which is referenced by obj1, and calls append method of this list, which modifies itself in place global obj1 obj1 = [] # 'a' still references to original list, which is [1] now, it have no relation to obj1 at all somefunc(obj1) -- With best regards, Daniel Kluev