Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #6523
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <crebert@ucsd.edu> |
| 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; 'languages,': 0.03; 'modified': 0.05; 'parameter': 0.05; 'refers': 0.07; 'snippet': 0.07; 'python': 0.08; 'declarations': 0.09; 'function:': 0.09; 'subject:parameters': 0.09; 'variables.': 0.09; 'output': 0.11; 'def': 0.12; 'am,': 0.14; 'debugging': 0.14; 'wrote:': 0.14; "['a": 0.16; 'adopted': 0.16; 'encountered.': 0.16; 'entirely.': 0.16; 'explanation': 0.16; 'function?': 0.16; 'illustrates': 0.16; 'made-up': 0.16; 'main():': 0.16; 'outputs': 0.16; 'subject:function': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; 'case.': 0.16; 'cc:addr:python-list': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'seems': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'here?': 0.23; 'received:209.85.213.46': 0.23; 'received:mail- yw0-f46.google.com': 0.23; 'objects': 0.23; 'code': 0.24; 'function': 0.25; 'changed': 0.25; 'parameters': 0.26; 'statement': 0.26; 'object': 0.26; 'example': 0.27; 'message- id:@mail.gmail.com': 0.28; 'changing': 0.28; 'problem': 0.28; 'effect': 0.29; 'explicitly': 0.29; 'cc:addr:python.org': 0.30; 'fact': 0.30; 'sun,': 0.30; 'print': 0.31; 'does': 0.33; 'list': 0.33; 'chris': 0.34; 'rule': 0.34; 'there': 0.35; 'function.': 0.35; 'received:132': 0.35; 'considered': 0.36; 'several': 0.36; 'uses': 0.36; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'received:209.85.213': 0.37; 'another': 0.37; 'model': 0.37; 'url:org': 0.38; 'but': 0.38; 'third': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'unless': 0.39; 'received:209': 0.39; 'spent': 0.39; 'missing': 0.40; 'worth': 0.60; 'more': 0.60; 'below': 0.61; 'order': 0.62; 'matter': 0.63; 'due': 0.67; 'url:htm': 0.71; 'either,': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=simple/simple; d=ucsd.edu; i=crebert@ucsd.edu; q=dns/txt; s=041709-iport; t=1306663323; x=1338199323; h=mime-version:in-reply-to:references:date:message-id: subject:from:to:cc:content-transfer-encoding; z=MIME-Version:=201.0|In-Reply-To:=20<F8395F78-615E-4FBD-B 6FC-1D6173EAEA45@mcgill.ca>|References:=20<F8395F78-615E- 4FBD-B6FC-1D6173EAEA45@mcgill.ca>|Date:=20Sun,=2029=20May =202011=2003:01:52=20-0700|Message-ID:=20<BANLkTi=3DUYmvG b3vW6GXgSx-pvwn0rayWgQ@mail.gmail.com>|Subject:=20Re:=20s cope=20of=20function=20parameters|From:=20Chris=20Rebert =20<crebert@ucsd.edu>|To:=20Henry=20Olders=20<henry.older s@mcgill.ca>|Cc:=20python-list@python.org |Content-Transfer-Encoding:=20quoted-printable; bh=nNGTAWH6s4x63/Jdc5G2eo/8ng1wP4wuYKuHrsSvcTM=; b=fYlyeyWkGcKI7FpjeGD5+orunytUg/vkJkSJQMevg69y2lp+a7Qf9gSK GvQDK8n4WzUJk7kGU0Y6F8n6hTZwQinMFwlCHQKQ4buxK2bESRD+RBY00 bZcheERhuqb92hVIRk5v4t4uzaXA42vYGNIXBBEl4AdPk97E1OSSBwMyC g=; |
| X-IronPort-Anti-Spam-Filtered | true |
| X-IronPort-Anti-Spam-Result | AhIFABYY4k2E7/kU/2dsb2JhbABVhEmhbwh3iHGiRItihlKIdIErg2yBBwSGZIlrhDCGdjuDWQ |
| X-IronPort-AV | E=Sophos;i="4.65,288,1304319600"; d="scan'208";a="533078392" |
| X-Spam-Status | No |
| X-Spam-Level | |
| MIME-Version | 1.0 |
| In-Reply-To | <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> |
| References | <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> |
| Date | Sun, 29 May 2011 03:01:52 -0700 |
| Subject | Re: scope of function parameters |
| From | Chris Rebert <crebert@ucsd.edu> |
| To | Henry Olders <henry.olders@mcgill.ca> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2218.1306663324.9059.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1306663324 news.xs4all.nl 49038 [::ffff:82.94.164.166]:46068 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:6523 |
Show key headers only | View raw
On Sun, May 29, 2011 at 1:30 AM, Henry Olders <henry.olders@mcgill.ca> 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.
To be more accurate, the list object referred to by `a` was modified
through c, due to the fact that a, b, and c all refer to the very same
object in this case.
> 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.
<snip>
> 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?
Function parameters *are* local variables. Function parameters are
indeed local in that *rebinding* them has no effect outside of the
function:
def foo(a):
a = 42
def bar():
b = 1
foo(b)
print b
bar() #=> outputs 1
As you've observed, *mutating* the object a variable refers to is
another matter entirely. Python does not use call-by-value and does
not copy objects unless explicitly requested to, as you've
encountered. But it does not use call-by-reference either, as my
example demonstrates. Like several other popular contemporary
languages, Python uses call-by-object for parameter passing; a good
explanation of this model can be found at
http://effbot.org/zone/call-by-object.htm It's well worth reading.
Cheers,
Chris
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: scope of function parameters Chris Rebert <crebert@ucsd.edu> - 2011-05-29 03:01 -0700
csiph-web