Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97083

Re: Idiosyncratic python

From jmp <jeanmichel@sequans.com>
Subject Re: Idiosyncratic python
Date 2015-09-24 20:04 +0200
References <560391ea$0$2885$c3e8da3$76491128@news.astraweb.com> <mu0eq4$2d2$1@ger.gmane.org> <CAJ4+4aoDY30HmhNrOjG2g+ODoS1vSOMtuANXp0unbuwWcuQd3g@mail.gmail.com> <mu102h$2oh$1@ger.gmane.org> <CALwzidk73ZnivKH8ajB0yFzBDc_s5mtvE283tjYjgRE2T2Px=Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.137.1443117871.28679.python-list@python.org> (permalink)

Show all headers | View raw


On 09/24/2015 04:26 PM, Ian Kelly wrote:
> On Thu, Sep 24, 2015 at 8:07 AM, jmp <jeanmichel@sequans.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Idiosyncratic python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-09-24 16:02 +1000
  Re: Idiosyncratic python Paul Rubin <no.email@nospam.invalid> - 2015-09-23 23:16 -0700
    Re: Idiosyncratic python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-09-24 16:35 +1000
      Re: Idiosyncratic python Ben Finney <ben+python@benfinney.id.au> - 2015-09-24 16:54 +1000
        Re: Idiosyncratic python Steven D'Aprano <steve@pearwood.info> - 2015-09-25 11:08 +1000
      Re: Idiosyncratic python Terry Reedy <tjreedy@udel.edu> - 2015-09-24 02:54 -0400
  Re: Idiosyncratic python wxjmfauth@gmail.com - 2015-09-24 00:06 -0700
    Re: Idiosyncratic python Laurent Pointal <laurent.pointal@free.fr> - 2015-09-24 19:50 +0200
      Re: Idiosyncratic python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-24 21:05 +0100
  Re: Idiosyncratic python jmp <jeanmichel@sequans.com> - 2015-09-24 11:12 +0200
  Re: Idiosyncratic python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-24 14:09 +0100
  Re: Idiosyncratic python jmp <jeanmichel@sequans.com> - 2015-09-24 16:07 +0200
  Re: Idiosyncratic python Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-24 08:26 -0600
  Re: Idiosyncratic python Chris Angelico <rosuav@gmail.com> - 2015-09-25 02:57 +1000
  Re: Idiosyncratic python jmp <jeanmichel@sequans.com> - 2015-09-24 20:04 +0200
  Re: Idiosyncratic python Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-24 12:19 -0600
  Re: Idiosyncratic python Ned Batchelder <ned@nedbatchelder.com> - 2015-09-24 13:46 -0700
    Re: Idiosyncratic python Laura Creighton <lac@openend.se> - 2015-09-24 23:08 +0200
    Re: Idiosyncratic python Chris Angelico <rosuav@gmail.com> - 2015-09-25 07:49 +1000
    Re: Idiosyncratic python Steven D'Aprano <steve@pearwood.info> - 2015-09-25 10:55 +1000
  Re: Idiosyncratic python sohcahtoa82@gmail.com - 2015-09-24 15:32 -0700
  Re: Idiosyncratic python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-25 00:40 +0100
  Re: Idiosyncratic python Akira Li <4kir4.1i@gmail.com> - 2015-09-25 03:04 +0300
  Re: Idiosyncratic python Steven D'Aprano <steve@pearwood.info> - 2015-09-25 10:08 +1000

csiph-web