Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'parameters': 0.04; 'output': 0.05; 'modify': 0.07; 'attributes': 0.09; 'item.': 0.09; 'python:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'callee': 0.16; 'dict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers,': 0.16; 'list),': 0.16; 'parameters,': 0.16; 'pointers,': 0.16; 'stats': 0.16; 'subject:variable': 0.16; 'tuple.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'passing': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'mon,': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'equivalent': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'usually': 0.31; '-0700,': 0.31; 'closer': 0.31; "d'aprano": 0.31; 'purely': 0.31; 'steven': 0.31; 'though.': 0.31; 'allows': 0.31; 'probably': 0.32; 'implemented': 0.33; 'maybe': 0.34; 'something': 0.35; 'case,': 0.35; 'operate': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'returning': 0.36; 'words,': 0.36; 'done': 0.36; 'doing': 0.36; 'list': 0.37; 'list.': 0.37; 'list,': 0.38; 'pm,': 0.38; 'how': 0.40; 'around.': 0.60; "you're": 0.61; 'times': 0.62; 'burning': 0.84; 'characters,': 0.84; 'common,': 0.84; 'environment;': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=5FbsA4KJCSTCHnPb/ZFnalj+ZG0Gla0qvygDizvvA80=; b=Pefed3nhm9ZpEJ9EHvk35E8izk4TjVURmJVS0BlI4N8HKLa8Kq36HkpIjhxnLssVQs QDUSJ+3KZPTU3biKw3ii9lMxCjbu8NJyqRe+hfo+Id84vkk4+o/DBSphjXDtAUOdWaZI qLozSiM8vqKdN5BrBbQF66yVGylmu8+UiAi2VugOyXTtj0ZKhB3YO7wJc3houwn8HkTD H4I2C5oVLWLvEjqaEvFlas0GljrdDWnY1WZz5VP6inM8nB70aIPcR0e2P4GjdZChy0sU rGvyBMLPPtb41qBLoV2ebJpQjKLq/Ef6DNfWxv2gpO9hfK0fynmenEQDn/PkdCOx7+E+ knOw== MIME-Version: 1.0 X-Received: by 10.58.160.134 with SMTP id xk6mr90158veb.64.1399369995439; Tue, 06 May 2014 02:53:15 -0700 (PDT) In-Reply-To: <87lhufi85o.fsf@elektro.pacujo.net> References: <53689aeb$0$11109$c3e8da3@news.astraweb.com> <87lhufi85o.fsf@elektro.pacujo.net> Date: Tue, 6 May 2014 19:53:15 +1000 Subject: Re: Pass variable by reference From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399369998 news.xs4all.nl 2917 [2001:888:2000:d::a6]:39536 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70951 On Tue, May 6, 2014 at 7:11 PM, Marko Rauhamaa wrote: > Steven D'Aprano : > >> On Mon, 05 May 2014 17:39:44 -0700, Satish Muthali wrote: >>> I have a burning question on how to pass variable by reference in >>> Python. I understand that the data type has to be mutable. >> >> [...] >> >> To get an effect *similar* to pass-by-reference, you can wrap your >> variable in a list, and then only operate on the list item. > > Consider also returning multiple values in a tuple. > > In C: > > stats_read(stats, &characters, &words, &lines); > > In Python: > > characters, words, lines = stats.read() That's not really pass-by-reference, though. What you're doing is output parameters, which are usually implemented in C with pointers, but in Python with a return tuple. Pass-by-reference allows the callee to see and modify something in the caller's environment; for instance, the stats_read() C function might maintain stats in the three pointed-to integers, eg incrementing them for each char/word/line processed. The Python equivalent would need to pass them as parameters AND return them. For that sort of case, you'd probably want to pass an object with three attributes (or maybe a dict or a list), which would then be modified; that's a much closer approximation of pass-by-reference. Hence Steven's statement about wrapping it in a list. And, by the way, it's not purely academic. There have been times when I've done exactly that as a means of passing state around. It's not common, but it has its place. ChrisA