Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'assign': 0.07; 'assignment': 0.07; 'function,': 0.09; 'mentions': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'subject:into': 0.09; 'things,': 0.09; 'variable,': 0.09; 'python': 0.11; 'def': 0.12; 'accesses': 0.16; 'assigns': 0.16; 'confuse': 0.16; 'function?': 0.16; 'parameters,': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'variable': 0.18; '(but': 0.19; 'passing': 0.19; 'seems': 0.21; 'python?': 0.22; 'header:User-Agent:1': 0.23; 'received:emailsrvr.com': 0.24; 'refers': 0.24; 'question': 0.24; 'received:(smtp server)': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'statement': 0.30; 'gary': 0.31; 'parameters.': 0.31; 'prints': 0.31; "can't": 0.35; 'but': 0.35; 'subject:?': 0.36; 'two': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'subject:Can': 0.60; 'back': 0.62; 'different': 0.65; 'sam': 0.68; 'computation.': 0.84; 'of*': 0.84 X-Virus-Scanned: OK Date: Fri, 21 Feb 2014 00:41:59 -0800 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Can global variable be passed into Python function? References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> In-Reply-To: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392972122 news.xs4all.nl 2872 [2001:888:2000:d::a6]:35722 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66814 On 02/20/2014 10:37 PM, 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? Are you passing a value *into* a function, or back *out of* the function? These are two very different things, and your question mentions both and seems to confuse them. You can pass any value into a function through its parameters, whether that value comes from a local variable, global variable, or the result of some computation. (But consider: it is the value that is being passed in -- not the variable that contains that value.) You can't pass values back out from a function through assignment to any of the parameters. The global statement is a way to allow a function to assign to a global variable, but this would not be considered "passing" a global into a function. It's just a way to access and assign to a global variable from withing a function. v = 123 def f(...): global v # Now v refers to the global v print(v) # Accesses and prints the global v v = 456 # Assigns to the global v. Gary Herron