Re: anything like C++ references?

Discussion in 'Python' started by Dave Brueck, Jul 13, 2003.

  1. Dave Brueck

    Dave Brueck Guest

    On Saturday 12 July 2003 04:56 pm, Ian Bicking wrote:
    > On Sat, 2003-07-12 at 17:45, Brian Quinlan wrote:
    > > > void change(int& i)
    > > > {
    > > > i++;
    > > > }

    > >
    > > The idiomatic way to write this code in python would be:
    > >
    > > def change(val):
    > > return val + 1

    >
    > To be more specific, you would achieve the same effect with:
    >
    > def change(val):
    > return val + 1
    > i = change(i)
    >
    > As opposed to the C++ where you'd do:
    >
    > change(i) // no assignment needed
    >
    >
    > There is no direct Python equivalent to the C++ function, for all sorts
    > of reasons (most of them very deliberate).


    Your response above (that the normal thing to do in Python is just return the
    modified value) made me wonder if most people are asking about
    pass-by-reference not because they want pass-by-reference, but because in C
    it's generally a nuisance to return stuff from functions, especially multiple
    values, so you end up learning about pointers and/or pass-by-reference.

    IOW, if you could erase the influence of previous languages would this FAQ
    become "how can I return multiple things from a function" more often than it
    would become "how can I modify an object from inside a function"?

    -Dave
    Dave Brueck, Jul 13, 2003
    #1
    1. Advertising

  2. Dave Brueck

    Tom Plunket Guest

    Dave Brueck wrote:

    > Your response above (that the normal thing to do in Python is
    > just return the modified value) made me wonder if most people are
    > asking about pass-by-reference not because they want pass-by-
    > reference, but because in C it's generally a nuisance to return
    > stuff from functions, especially multiple values, so you end up
    > learning about pointers and/or pass-by-reference.


    In C++ it's trivial to return multiple values and pass-by-
    reference is typically (at least, as far as I've seen) used to
    pass immutables anyway, but sometimes it just seems like the
    right tool for the job.

    > IOW, if you could erase the influence of previous languages would
    > this FAQ become "how can I return multiple things from a
    > function" more often than it would become "how can I modify an
    > object from inside a function"?


    That's a good idea, although I would have to say that multiple
    return values typically means that your function is doing too
    many things. ;)

    -tom!
    Tom Plunket, Jul 13, 2003
    #2
    1. Advertising

  3. Dave Brueck

    Ian Bicking Guest

    On Sun, 2003-07-13 at 08:56, Stephen Horne wrote:>
    > You are not directly returning multiple values - you are returning a
    > tuple. You can return containers in C++ too. The only difference is
    > that Python makes working with tuples *much* easier


    Of course, anything you can do in Python you can do in any other
    language. The *whole point* is that Python is so much easier. Don't
    worry so much about every equivalent feature or about translating every
    C++ idiom to Python. Just write your programs -- they'll be different
    from C++, but that's okay.

    Ian
    Ian Bicking, Jul 13, 2003
    #3
  4. Stephen Horne <> writes:

    > On Sun, 13 Jul 2003 05:50:46 -0600, Dave Brueck
    > <> wrote:
    >
    > >But the method for returning more than one thing is not a simple progression
    > >from this pattern. Instead you learn and shift to a *completely* different
    > >mechanism. In Python, however, you *can* continue along the original route as
    > >well:
    > >
    > >def foo(a, b, c):
    > > return a+b, b+c

    >
    > You are not directly returning multiple values - you are returning a
    > tuple. You can return containers in C++ too. The only difference is
    > that Python makes working with tuples *much* easier - but its no more
    > a 'simple progression' than in C++. You still need to know about
    > tuples as well as returning values.


    Actually, at one stage in Python's development it was almost
    completely symmetric: you (always) passed in a tuple, and could
    optionally return a tuple. The syntax made it look a bit like you
    were passing in multiple arguments, but under the hood a tuple was
    what you were passing, and there were ways in which this showed up.
    IIRC,

    f((a, b))

    and

    f(a, b)

    were indistinguishable from inside f. This was all long before I came
    across python, so I may be getting things mixed up...

    Cheers,
    M.

    --
    Roll on a game of competetive offence-taking.
    -- Dan Sheppard, ucam.chat
    Michael Hudson, Jul 14, 2003
    #4
  5. Stephen Horne <> wrote in message news:<>...
    > >def foo(a, b, c):
    > > return a+b, b+c

    >
    > You are not directly returning multiple values - you are returning a
    > tuple. You can return containers in C++ too. The only difference is
    > that Python makes working with tuples *much* easier - but its no more
    > a 'simple progression' than in C++. You still need to know about
    > tuples as well as returning values.


    You don't need to know about tuples so that you can understand
    that multiple values are returned, and that's the difference.
    I didn't know of tuples when I saw some of my first Python code,
    yet the idiom for returning multiple values was trivially clear.
    It became even better though when I learned it was implemented
    through tuples ;).

    In contrast, in C++ one needs to explicitely state make_pair
    or make_tuple, requiring the user to know of the underlying
    types.

    Had I been required to type

    def foo(a, b, c):
    return tuple(a+b, b+c)

    x = foo(a, b, c)
    a = x[0]
    b = x[1]

    Then I could've agreed with you. But multi-assignment from
    tuples and implicit creation of tuples makes the returning
    of multiple values look like a language feature, not a way
    to abuse tuples. This ease of use is the reason it can work
    as a replacement for C++'s "send-by-reference".
    =?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=, Jul 14, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Bicking

    RE: anything like C++ references?

    Ian Bicking, Jul 12, 2003, in forum: Python
    Replies:
    54
    Views:
    1,071
    Christos TZOTZIOY Georgiou
    Jul 22, 2003
  2. David McNab

    Re: anything like C++ references?

    David McNab, Jul 13, 2003, in forum: Python
    Replies:
    19
    Views:
    498
    Christos TZOTZIOY Georgiou
    Jul 18, 2003
  3. Tim Peters

    RE: anything like C++ references?

    Tim Peters, Jul 13, 2003, in forum: Python
    Replies:
    44
    Views:
    1,036
    Tim Roberts
    Jul 20, 2003
  4. Michael Chermside

    RE: anything like C++ references?

    Michael Chermside, Jul 14, 2003, in forum: Python
    Replies:
    2
    Views:
    260
    Michael Hudson
    Jul 15, 2003
  5. Replies:
    5
    Views:
    310
    Stephen Horne
    Jul 17, 2003
Loading...

Share This Page