Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!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; 'variables': 0.07; 'objects,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'thus,': 0.09; 'variable,': 0.09; 'python': 0.11; 'def': 0.12; '"x"': 0.16; 'formerly': 0.16; 'objects.': 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; 'variable': 0.18; '(where': 0.19; 'have:': 0.19; 'appears': 0.22; 'python?': 0.22; 'header:User-Agent:1': 0.23; 'this:': 0.26; 'pass': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'writes:': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'charset:us- ascii': 0.36; 'subject:?': 0.36; 'implement': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'subject:Can': 0.60; 'received:217': 0.63; 'name': 0.63; 'become': 0.64; 'different': 0.65; 'sam': 0.68; 'self.value': 0.84; 'notion': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: Can global variable be passed into Python function? Date: Fri, 21 Feb 2014 08:23:19 +0100 References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e0b35e.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:o5DW/x+u+DbRLQltlUUht7r5ij8= 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392967413 news.xs4all.nl 2866 [2001:888:2000:d::a6]:54145 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66807 Sam writes: > I need to pass a global variable into a python function. Python does not really have the concept "variable". What appears to be a variable is in fact only the binding of an object to a name. If you assign something to a variable, all you do is binding a different object to the name. Thus, if you have: i = 1 def f(x): x = 5 f(i) Then "i" will remain "1" and not become "5". The effect of "x = 5" is that the name "x" gets bound to "5" (where is formerly was bound to "1"). >However, the global variable does not seem to be assigned after the function ends. Is it because parameters are not passed by reference? Python lacks the notion of "variable". Thus, it does not pass variables into functions but objects. The objects, however, get passed by reference. >How can I get function parameters to be passed by reference in Python? You can implement your own concept of variable, e.g. like this: class Variable(object): def __init__(self, value): self.value = value The little program above then becomes: i = Variable(1) def f(x): x.value = 5 f(i) i.value # this is now 5