Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34214

scope, function, mutable

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news0.firedrake.org!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gusarer@gmail.com>
X-Original-To Python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'skip:[ 20': 0.03; 'python': 0.09; '&quot;)': 0.09; '[1,': 0.09; 'below).': 0.09; 'def': 0.10; '2.7': 0.13; '(like': 0.15; 'assignment.': 0.16; 'appropriate': 0.20; 'variable': 0.20; 'changes': 0.20; 'assignment': 0.22; 'skip:[ 10': 0.26; '(see': 0.27; 'functions.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'definition': 0.29; 'becomes': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'but': 0.36; 'thank': 0.36; 'received:209': 0.37; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'from:no real name:2**0': 0.60; 'you.': 0.61; 'subject:, ': 0.61; 'behavior': 0.64
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=o7QZGLz3l93eSXbT2kibxEdgoFn7dnMuL87Na6i/MrI=; b=jlPa8d8ikjbzCadfmuefZk0X9BoqV2vVGoaifYduDd+g1Jw6UcxDqdaNsfRzo1yr3E Qa+6m6jUxs3p/zRnMn7Ess7R4NxmbVcdFU6d9uD0Fa4A90bCcAVG1HwtKFCR3ploQA1+ +KQRAKyL2rmoCYqw9EGNBrgW0YWCVh8g1cuTerLucS/p/AzV0SicZVZHORvP5Zz4Q5A5 Kjg05LH/8VY34IRQdpUnF43muKqGydEEmK7LX6oQRCMUgO+j67zEvEAQuke+Dc72Bnk9 OAAdvYkjZQVZRHXPGWOPOX1Ev+s6JsM9gq6nfdIF7tDl8PUGnf+3+8DPVvsGQvhQJaSx NOUA==
MIME-Version 1.0
Date Tue, 4 Dec 2012 14:55:44 +0400
Subject scope, function, mutable
From gusarer@gmail.com
To Python-list@python.org
Content-Type multipart/alternative; boundary=14dae93408572eee8f04d004b5b7
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.455.1354618548.29569.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354618548 news.xs4all.nl 6918 [2001:888:2000:d::a6]:35219
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34214

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi,

What is the appropriate definition for the following behavior in Python 2.7
(see code below).
Both functions have assignment in it (like "x = ") so I assume, that x is a
local variable in both functions.
Also I thought this rule doesn't depend on WHERE in this function we find
the assignment.

But in both functions x becomes local variable only AFTER assignment, so in
f2 x[1] = 99 changes the global varialble (btw, x[3] = 99 doesn't).

def f1(x):
    x = [44] + x[1:]
    x[1] = 99

def f2(x):
    x[1] = 99
    x = [44] + x[1:]
    x[3] = 99

t = [1,2,3,4,5,6,7]
f1(t)
print t                            # [1, 2, 3, 4, 5, 6, 7]
t = [1,2,3,4,5,6,7]
f2(t)
print t                            # [1, 99, 3, 4, 5, 6, 7]

Thank you.

Roman Gusarev.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

scope, function, mutable gusarer@gmail.com - 2012-12-04 14:55 +0400
  Re: scope, function, mutable Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-04 11:32 +0000
  Re: scope, function, mutable Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-12-04 13:54 +0200

csiph-web