Path: csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!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; 'value,': 0.03; 'caller': 0.07; 'option,': 0.07; 'malloc': 0.09; 'naturally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'python.': 0.11; 'stack': 0.13; 'subject:python': 0.14; 'thu,': 0.15; 'weird': 0.15; '24,': 0.16; ':o)': 0.16; 'c++.': 0.16; 'fits': 0.16; 'heap': 0.16; 'jmp': 0.16; 'pointer,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'pointer': 0.18; 'creates': 0.18; 'variable': 0.18; '2015': 0.20; 'context.': 0.22; 'expense': 0.22; 'sep': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'allocated': 0.27; 'function': 0.28; 'pointer.': 0.29; "i'm": 0.30; 'code': 0.30; 'option': 0.31; 'returned': 0.32; 'structure': 0.34; 'gives': 0.35; 'something': 0.35; 'but': 0.36; 'structures': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'copying': 0.38; 'data': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'provide': 0.61; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'course': 0.62; 'leaving': 0.63; 'within': 0.64; 'expert': 0.70; 'habit': 0.91; 'leak': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: jmp Subject: Re: Idiosyncratic python Date: Thu, 24 Sep 2015 20:04:17 +0200 References: <560391ea$0$2885$c3e8da3$76491128@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: paris.sequans.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443117871 news.xs4all.nl 23811 [2001:888:2000:d::a6]:37510 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97083 On 09/24/2015 04:26 PM, Ian Kelly wrote: > On Thu, Sep 24, 2015 at 8:07 AM, jmp wrote: >> result = getResult() >> >> For the later, the original weird form come from a C habit to allocate >> returned structures within the caller and provide a pointer to it so the >> function can fill the data in, otherwise the structure is lost as the stack >> is popped out and the structure content is garbage. None of this make any >> sense in python. > > Only if the structure is allocated on the stack and returned by > pointer. If it's returned by value, then the content remains intact, > but at the expense of copying it. The other option of course would be > to allocate it on the heap and return the pointer, but this needlessly > incurs malloc overhead and creates an opportunity for a memory leak if > the variable is naturally stack-scoped. Python effectively takes this > option, as everything is allocated on the heap. Leaving it up to the > caller to provide a pointer also gives the caller the option of > allocating on the stack or the heap as best fits the context. > I'm not an expert but I think this "return by value thing" is only for C++. In vintage C, you can only return something that fits within a register. Anyway, there's a lot of legit C code in which functions are plagued by 'out parameters' and somehow it has transpired in some python code for no reason :o) jm