Python Args By Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Edwards

    #16
    Re: Python Args By Reference

    On 2005-05-11, Peter Hansen <peter@engcorp. com> wrote:
    [color=blue][color=green]
    >> The most common immutable objects you'll see are strings and
    >> tuples, and the main reason they're immutable is to allow them
    >> to be dict keys.[/color]
    >
    > And ints. Strings, tuples and ints are the *three* most
    > common immutable objects you'll see...[/color]

    Right. In Python, strings tuples and ints are the five most
    common immutable objects you'll see.

    --
    Grant Edwards grante Yow! You were s'posed
    at to laugh!
    visi.com

    Comment

    • ncf

      #17
      Re: Python Args By Reference

      Thanks to everyone for your assistance. I shall reread this a couple
      times and then try to make something that works.

      Many thanks and have a GREAT day.
      -Wes

      Comment

      • Terry Hancock

        #18
        Re: Python Args By Reference

        On Wednesday 11 May 2005 08:43 am, Peter Hansen wrote:[color=blue]
        > Roy Smith wrote:[color=green]
        > > The most common immutable objects you'll see are strings and tuples, and
        > > the main reason they're immutable is to allow them to be dict keys.[/color]
        >
        > And ints. Strings, tuples and ints are the *three* most common
        > immutable objects you'll see...[/color]

        And floats. Floats, strings, tuples, and ints art the *four* most common
        immutable objects you'll see...

        Time to break out the comfy chair. ;-)
        --
        Terry Hancock ( hancock at anansispacework s.com )
        Anansi Spaceworks http://www.anansispaceworks.com

        Comment

        • Fredrik Lundh

          #19
          Re: Python Args By Reference

          Grant Edwards wrote:
          [color=blue]
          > Yes. All arguments are passed by reference. This must be in
          > the FAQ somewhere...[/color]

          hopefully not, because that saying that "all arguments are passed
          by reference" is extremely confusing for people who've learned about
          "call by reference" in school.

          as has discussed many times on this list, using "call by object"
          (or perhaps "call by object reference" or even "call by sharing")
          is better, both for CS heads and for ordinary people.

          see e.g.





          from a thread where someone claimed that Python used "call by value"
          (which is true, as long as you're free to use your own definition of the
          word "value" ;-)

          </F>



          Comment

          • Terry Reedy

            #20
            Re: Python Args By Reference


            "ncf" <nothingcanfulf ill@gmail.com> wrote in message
            news:1115787330 .723188.295610@ f14g2000cwb.goo glegroups.com.. .[color=blue]
            > Example C Code:
            > #define P(a,b,c,d,e,f,g ,h,x,K) \
            > { \
            > temp1 = h + S3(e) + F1(e,f,g) + K + x; \
            > temp2 = S2(a) + F0(a,b,c); \
            > d += temp1; h = temp1 + temp2; \
            > }[/color]
            [color=blue]
            > Python Code:
            > def P(a,b,c,d,e,f,g ,h,x,K):
            > temp1 = h + S3(e) + F1(e,f,g) + K + x
            > temp2 = S2(a) + F0(a,b,c)
            > d += temp1; h = temp1 + temp2[/color]

            Your problem is that you are replacing a C macro, which is inlined and
            affects the original 'namespace' in which it appears, and which does not
            have a direct Python equivalent, with a Python function (admittedly
            slower), which performs its calculation in a separate namespace. So you
            have to pass the results back to the calling namespace. Simple enough:
            make the last line of P

            return d+temp1, temp1+temp2

            and the call

            a4,a8 = P(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)

            The stuff about mutability and calling conventions is important to learn
            eventually but seems beside the point for your immediate application.

            Terry J. Reedy



            Comment

            • Steve Holden

              #21
              Re: Python Args By Reference

              Fredrik Lundh wrote:[color=blue]
              > Grant Edwards wrote:
              >
              >[color=green]
              >>Yes. All arguments are passed by reference. This must be in
              >>the FAQ somewhere...[/color]
              >
              >
              > hopefully not, because that saying that "all arguments are passed
              > by reference" is extremely confusing for people who've learned about
              > "call by reference" in school.
              >
              > as has discussed many times on this list, using "call by object"
              > (or perhaps "call by object reference" or even "call by sharing")
              > is better, both for CS heads and for ordinary people.
              >
              > see e.g.
              >
              > http://mail.python.org/pipermail/pyt...ay/163312.html
              > http://mail.python.org/pipermail/pyt...ay/163028.html
              > http://mail.python.org/pipermail/pyt...ay/163078.html
              >
              > from a thread where someone claimed that Python used "call by value"
              > (which is true, as long as you're free to use your own definition of the
              > word "value" ;-)
              >[/color]
              Or, as one might say, '''"call by value" for some value of "value"'''.

              regards
              Steve
              --
              Steve Holden +1 703 861 4237 +1 800 494 3119
              Holden Web LLC http://www.holdenweb.com/
              Python Web Programming http://pydish.holdenweb.com/

              Comment

              Working...