Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'value,': 0.03; 'caller': 0.07; 'function,': 0.07; 'subject:question': 0.08; 'assigning': 0.09; 'global,': 0.09; 'parameter.': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'value.': 0.15; '"right"': 0.16; '12:07,': 0.16; 'not;': 0.16; 'top-level': 0.16; 'string': 0.17; 'wrote:': 0.17; 'code,': 0.18; 'subject:] ': 0.19; 'modifying': 0.22; 'of.': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'correct': 0.28; 'modified,': 0.29; 'case,': 0.29; 'structure': 0.32; 'subject:lists': 0.32; 'point,': 0.33; 'third': 0.34; 'returning': 0.35; 'there': 0.35; 'level.': 0.36; 'level': 0.37; 'rather': 0.37; 'object': 0.38; 'some': 0.38; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'more': 0.63; 'managing': 0.64; 'taking': 0.65; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'as:': 0.75; 'received:74.208.4.194': 0.84 Date: Fri, 10 Aug 2012 06:53:19 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Mok-Kong Shen Subject: Re: [newbie] A question about lists and strings References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:qXng+9kUuJ5+s/nRXk/OfVEm5AkycgK6veAhZOW/0sH Elwjcdq4SYCG1VJe6JPyJiOBXlabk7+MEUEQeswER3jk1mu1N8 yNRFe/FN2cKipUMUViKfaL6REaGcZtmJphyejzixmellfNglxQ tfSwTr0kv6oKnrLYnA9EaGq6YjJxCHHeSdkN3M96Zj+3zn8FXg nOodgsOceWBkq953VVFzTpabg1e9lAZhR/K9030HwmmbMh8jPa w8RY2dhfwOYo/r8wTb3nW16lfmNA148vAsX20YOnxAKFcxFA3A ULmeB2b1r75cJI76BAFc09LE4791982tS0x4x7bHQriWurz9A= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name 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: 2001:888:2000:d::a6 X-Trace: 1344596023 news.xs4all.nl 6983 [2001:888:2000:d::a6]:40959 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26860 On 08/10/2012 06:31 AM, Mok-Kong Shen wrote: > Am 10.08.2012 12:07, schrieb Dave Angel: > [snip] >> At this point, in top-level code, the listb object has been modified, >> and the strb one has not; it still is bound to the old value. > > This means there is no way of modifying a string at the top level > via a function, excepting through returning a new value and assigning > that to the string name at the top level. Please again correct me, if > I am wrong. > > M. K. Shen > You're close. There are three ways I can think of. The "right" way is to return a value, which the caller can use any way he wants, including binding it to a global. Second is to declare the name as global, rather than taking the object as a formal parameter. In this case, you're taking on the responsibility for managing that particular global, by its correct name. def yy(): global strb strb += "whatever" Third is to hold the string in some more complex structure which is mutable. (untested, may contain typos) def zz(mydict): mydict["key1"] += "text" called as: globaldict = {"key1": "initial ", "key2": "init"} -- DaveA