Reference Variables In Python Like Those In PHP

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

    #1

    Reference Variables In Python Like Those In PHP

    Is It possible to have reference variables like in PHP

    ex.

    <?php

    $x = 1;
    $y =& $x;

    $y += 1;

    echo $x;
    echo "\n"
    echo $y;

    ?>

    This would show

    2
    2

    Is this available in python?

  • Terry Reedy

    #2
    Re: Reference Variables In Python Like Those In PHP


    "Chaos" <psnim2000@gmai l.comwrote in message
    news:1155651471 .605912.46640@m 73g2000cwd.goog legroups.com...
    Is It possible to have reference variables like in PHP
    <?php
    $x = 1;
    $y =& $x;
    $y += 1;
    echo $x;
    echo "\n"
    echo $y;
    ?>
    >
    This would show
    2
    2
    Is this available in python?
    No, in the literal meaning of your question. But..
    1. The concept 'reference variable' pertains to PHP's object model, which
    is different from Python's.
    2. They are means, not ends. So a possibly more useful question would be
    along the lines of "In PHP, I do this with a reference variable. How can I
    accomplish the same goal in Python?"

    Terry Jan Reedy



    Comment

    • Neil Cerutti

      #3
      Re: Reference Variables In Python Like Those In PHP

      On 2006-08-15, Chaos <psnim2000@gmai l.comwrote:
      Is It possible to have reference variables like in PHP
      >
      ex.
      >
      ><?php
      >
      $x = 1;
      $y =& $x;
      >
      $y += 1;
      >
      echo $x;
      echo "\n"
      echo $y;
      >
      ?>
      >
      This would show
      >
      2
      2
      >
      Is this available in python?
      If you store an item in a one-element list, you can use the list
      like a reference, but it's not syntactically transparent.
      >>x = [1]
      >>y = x
      Now x and y refer to the same list. Now you can change the
      elements in the list through y.
      >>y[0] += 1
      >>x
      [2]
      >>y
      [2]

      --
      Neil Cerutti
      Sermon Outline: I. Delineate your fear II. Disown your fear III.
      Displace your rear --Church Bulletin Blooper

      Comment

      • Luke Plant

        #4
        Re: Reference Variables In Python Like Those In PHP


        Chaos wrote:
        Is It possible to have reference variables like in PHP
        ....
        Is this available in python?
        You should note that, to a nearest equivalent, all variables are
        reference variables in Python. The difference is in what assignment
        does - += in Python does an assignment of a new object for immutable
        objects. For mutable objects like lists, += does an in place
        modification.

        x = 1
        y = x # y and x now point to the same object
        y += 1 # not any more, because ints are immutable,
        # and += is defined not to mutate for ints

        Luke

        Comment

        • Fredrik Lundh

          #5
          Re: Reference Variables In Python Like Those In PHP

          Luke Plant wrote:
          You should note that, to a nearest equivalent, all variables are
          reference variables in Python. The difference is in what assignment
          does - += in Python does an assignment of a new object for immutable
          objects. For mutable objects like lists, += does an in place
          modification.
          that depends on the object implementation, though -- the "+=" statement
          does not, in itself, distinguish between mutable and immutable objects.

          </F>

          Comment

          • Laurent Pointal

            #6
            Re: Reference Variables In Python Like Those In PHP

            Chaos a écrit :
            Is It possible to have reference variables like in PHP
            >
            ex.
            >
            <?php
            >
            $x = 1;
            $y =& $x;
            >
            $y += 1;
            >
            echo $x;
            echo "\n"
            echo $y;
            >
            ?>
            >
            This would show
            >
            2
            2
            >
            Is this available in python?
            See other replies (ie. in Python all variables are names refering to
            objects).

            You may achieve same result using an object as a namespace, like this:

            Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
            win32
            Type "help", "copyright" , "credits" or "license" for more information.
            >>class Dummy(object) : pass
            ....
            >>x=Dummy()
            >>x.val = 1
            >>y = x <-- here x and y refer to *same* Dummy object
            >>y.val += 1
            >>x.val
            2
            >>y.val
            2

            A+

            Laurent.

            Comment

            Working...