Reference?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kris Caselden

    Reference?

    I've searched all the Python docs I could find, but I haven't seen any
    mention of referencing function arguments, such as you would with the
    & in c/c++. Is this possible in Python?
  • Peter Otten

    #2
    Re: Reference?

    Kris Caselden wrote:
    [color=blue]
    > I've searched all the Python docs I could find, but I haven't seen any
    > mention of referencing function arguments, such as you would with the
    > & in c/c++. Is this possible in Python?[/color]

    The analog of the Python way of handling args among the C-style languages is
    Java. So, no, you are on mission impossible :-)

    class Mutable:
    pass

    v1 = Mutable()
    v1.name = "v1"
    v2 = "v2" #strings are immutable

    def fun(a1, a2):
    a1.name = "a1"
    a2 = "a2"
    fun(v1, v2)

    print v1.name # prints a1
    print v2 # prints v2

    As a workaround, you can do:

    def fun2():
    a1 = Mutable()
    a1.name = "A1"
    return a1, "A2"

    v1, v2 = fun2()

    print v1.name # prints A1
    print v2 # prints A2

    See the tutorial (http://www.python.org/doc/current/tut/node6.html) for the
    tricks you *can* do with function arguments

    Peter

    Comment

    • Peter Otten

      #3
      Re: Reference?

      Kris Caselden wrote:
      [color=blue]
      > I've searched all the Python docs I could find, but I haven't seen any
      > mention of referencing function arguments, such as you would with the
      > & in c/c++. Is this possible in Python?[/color]

      The analog of the Python way of handling args among the C-style languages is
      Java. So, no, you are on mission impossible :-)

      class Mutable:
      pass

      v1 = Mutable()
      v1.name = "v1"
      v2 = "v2" #strings are immutable

      def fun(a1, a2):
      a1.name = "a1"
      a2 = "a2"
      fun(v1, v2)

      print v1.name # prints a1
      print v2 # prints v2

      As a workaround, you can do:

      def fun2():
      a1 = Mutable()
      a1.name = "A1"
      return a1, "A2"

      v1, v2 = fun2()

      print v1.name # prints A1
      print v2 # prints A2

      See the tutorial (http://www.python.org/doc/current/tut/node6.html) for the
      tricks you *can* do with function arguments

      Peter

      Comment

      • Michael Peuser

        #4
        Re: Reference?


        "Kris Caselden" <google@hanger. snowbird.net> schrieb im Newsbeitrag
        news:abc3fdd3.0 308230057.6b309 [email protected] gle.com...
        [color=blue]
        > I've searched all the Python docs I could find, but I haven't seen any
        > mention of referencing function arguments, such as you would with the
        > & in c/c++. Is this possible in Python?[/color]

        Your question is probably not as sophisticated: Parameter passing in Python
        is *always* like & in C++.

        There had been an exaustive disyussion some weeks ago: here is my
        contribution:

        Sent: Wednesday, August 13, 2003 9:12 AM
        Subject: Re: two quick questions

        [color=blue]
        > The following examples might clear the more theoretical elaborations .....
        >
        >
        > def noUse(a):
        > a=(4,5,6)
        >
        > def tricky(a):
        > a[0]=(7,8,9)
        >
        > # case 1
        > x=[1,2,3]
        > print x
        > tricky(x)
        >
        > x=(1,2,3)
        > # case 2
        > noUse(x)
        > print x
        >
        > # case 3
        > tricky([x])
        > print x
        >
        > # case 4
        > y=[x]
        > tricky (y)
        > print x
        > print y[0]
        >
        > # case 5
        > tricky(x)
        > print x
        >
        >
        > Kindly
        > Michael Peuser[/color]


        Comment

        • Michael Peuser

          #5
          Re: Reference?


          "Kris Caselden" <google@hanger. snowbird.net> schrieb im Newsbeitrag
          news:abc3fdd3.0 308230057.6b309 [email protected] gle.com...
          [color=blue]
          > I've searched all the Python docs I could find, but I haven't seen any
          > mention of referencing function arguments, such as you would with the
          > & in c/c++. Is this possible in Python?[/color]

          Your question is probably not as sophisticated: Parameter passing in Python
          is *always* like & in C++.

          There had been an exaustive disyussion some weeks ago: here is my
          contribution:

          Sent: Wednesday, August 13, 2003 9:12 AM
          Subject: Re: two quick questions

          [color=blue]
          > The following examples might clear the more theoretical elaborations .....
          >
          >
          > def noUse(a):
          > a=(4,5,6)
          >
          > def tricky(a):
          > a[0]=(7,8,9)
          >
          > # case 1
          > x=[1,2,3]
          > print x
          > tricky(x)
          >
          > x=(1,2,3)
          > # case 2
          > noUse(x)
          > print x
          >
          > # case 3
          > tricky([x])
          > print x
          >
          > # case 4
          > y=[x]
          > tricky (y)
          > print x
          > print y[0]
          >
          > # case 5
          > tricky(x)
          > print x
          >
          >
          > Kindly
          > Michael Peuser[/color]


          Comment

          • mackstann

            #6
            Re: Reference?

            On Sat, Aug 23, 2003 at 01:57:33AM -0700, Kris Caselden wrote:[color=blue]
            > I've searched all the Python docs I could find, but I haven't seen any
            > mention of referencing function arguments, such as you would with the
            > & in c/c++. Is this possible in Python?[/color]

            Python is pass-by-reference by default.
            [color=blue][color=green][color=darkred]
            >>> f = []
            >>> def foo(n):[/color][/color][/color]
            .... n.append("hi")
            ....[color=blue][color=green][color=darkred]
            >>> foo(f)
            >>> print f[/color][/color][/color]
            ['hi']

            --
            m a c k s t a n n mack @ incise.org http://incise.org
            A bore is someone who persists in holding his own views after we have
            enlightened him with ours.

            Comment

            Working...