Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'parameters': 0.04; 'subject:Python': 0.06; 'ugly': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'python': 0.11; 'def': 0.12; '"x")': 0.16; '(1,': 0.16; 'name)': 0.16; 'name):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'import': 0.22; 'python?': 0.22; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'code': 0.31; 'there': 0.35; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'subject:Can': 0.60; 'sam': 0.68 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Can global variable be passed into Python function? Date: Fri, 21 Feb 2014 08:34:47 +0100 Organization: None References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdbb48.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392968086 news.xs4all.nl 2952 [2001:888:2000:d::a6]:34821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66808 Sam wrote: > I need to pass a global variable into a python function. However, the > global variable does not seem to be assigned after the function ends. Is > it because parameters are not passed by reference? How can I get function > parameters to be passed by reference in Python? If the variable you want to process is always the same global variable you can declare it: >>> x = 0 >>> def inc_x(): ... global x ... x += 1 ... >>> x 0 >>> inc_x() >>> x 1 >>> inc_x(); inc_x() >>> x 3 If you want to change ("rebind") varying global names you have to be explicit: >>> def inc(x): ... return x + 1 ... >>> x = 0 >>> x = inc(x) >>> y = 0 >>> y = inc(y) >>> y = inc(y) >>> x, y (1, 2) Then there are ugly hacks that make your code hard to follow. Don't use these: >>> def inc(name): ... globals()[name] += 1 ... >>> inc("x") >>> inc("x") >>> x 3 >>> def inc(module, name): ... setattr(module, name, getattr(module, name) + 1) ... >>> import __main__ >>> inc(__main__, "x") >>> inc(__main__, "x") >>> inc(__main__, "x") >>> x 6