Finding the instance reference of an object

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

    Re: Finding the instance reference of an object

    Marc 'BlackJack' Rintsch wrote:
    You have said the value that is copied is a pointer to the object.
    This assumes that "call by value" means "call by copying
    the value".

    That assumption is WRONG.

    It doesn't mean that. It means "call by ASSIGNING the
    value."

    So, you can think of the value of an expression as being
    an object, as opposed to a reference to an object, if
    you want. But then you need to consider what it means
    to assign that value to a name.

    Obviously it doesn't mean copying the value -- it must
    mean making the name refer to the value somehow.

    The same thing happens when the value is passed to a
    function.

    --
    Greg

    Comment

    • greg

      Re: Finding the instance reference of an object

      Joe Strout wrote:
      On Nov 5, 2008, at 2:06 PM, Lie wrote:
      >
      >Another example: pass-by-object.
      >
      Here's where we depart, I guess. I think there's no such thing (see
      <http://en.wikipedia.org/wiki/Evaluation_strategy for example, and the
      dead-tree references I have on hand agree).
      Something has just occurred to me. If you take the
      view that the value of an expression is an object,
      then the terms "value" and "object" are synonymous.

      So if you then insist that Python uses "call by object",
      you're actually saying it uses call by value!

      --
      Greg

      Comment

      • Steven D'Aprano

        Re: Finding the instance reference of an object

        On Sat, 08 Nov 2008 18:31:47 +1300, greg wrote:
        Marc 'BlackJack' Rintsch wrote:
        >
        >You have said the value that is copied is a pointer to the object.
        >
        This assumes that "call by value" means "call by copying the value".
        >
        That assumption is WRONG.
        Not according to my Comp Sci lecturers back in the day, and not according
        to my Pascal books.

        E.g. "Programmin g In Pascal", 2nd Edition (1985), by Peter Grogono of
        Concordia University refers to "value" parameters and "variable"
        parameters to refer to the standard Pascal call-by-value convention and
        the call-by-reference convention you get when you declare a parameter
        with the var keyword. He writes:

        "When an object is passed to a procedure by value, a local copy of it is
        made. If the object is a large array, the copying operation will make the
        program slower and will increase its memory requirements."

        This was written as an undergraduate textbook. Remember that in 1985, OOP
        was far more exotic than now, and object in the above means any Pascal
        type (integer, array, set etc.) and not object as we understand it today.

        Grogono doesn't explicitly use the terms "call-by-value" and "call-by-
        reference", but my lecture notes from the time make it clear that the
        Melbourne University Comp Sci department understood those terms to imply
        Pascal calling semantics.

        It doesn't mean that. It means "call by ASSIGNING the value."
        Which, in languages like Pascal, means COPYING the value. And that leads
        to confusion when people who understand C-B-V to mean what Pascal means
        by the term hear that Python is C-B-V.

        So, you can think of the value of an expression as being an object, as
        opposed to a reference to an object, if you want.
        That's the natural interpretation based on the ordinary meaning of the
        word "value". In a language like Python that doesn't have references,
        it's the only sensible interpretation. Otherwise you're forced to argue
        that following a statement x=1, the value of x is something that has no
        existence in Python code.

        But then you need to
        consider what it means to assign that value to a name.
        But you need to do that anyway.

        Here's a simple C program:

        struct Rec
        {
        int x;
        };

        struct Rec a;
        struct Rec b;

        int main(void)
        {
        a.x = 1;
        b = a;
        printf("Before: %d %d\n", a.x, b.x);
        a.x += 1;
        printf("After: %d %d\n", a.x, b.x);
        return 0;
        }


        It prints:

        Before: 1 1
        After: 2 1



        Here's a simple Python equivalent:

        class Rec:
        pass

        a = Rec()
        a.x = 1
        b = a
        print "Before: %d %d" % (a.x, b.x)
        a.x += 1
        print "After: %d %d" % (a.x, b.x)



        It prints:


        Before: 1 1
        After: 2 2



        Anyone want to argue that assignment in Python is the same as in C?


        Obviously it doesn't mean copying the value -- it must mean making the
        name refer to the value somehow.
        There's no "obviously" about it. To anyone who has learned that "call-by-
        value" means that a copy is made, "obviously" it does mean copying the
        value. If you have learned a different meaning, then you will believe
        differently.

        The same thing happens when the value is passed to a function.
        Sure. Calling conventions in Python are the same as name/value binding,
        except that you have a slight extra complication due to having two
        namespaces instead of one.



        --
        Steven

        Comment

        • Steven D'Aprano

          Re: Finding the instance reference of an object

          On Sat, 08 Nov 2008 19:30:17 +1300, greg wrote:
          Something has just occurred to me. If you take the view that the value
          of an expression is an object, then the terms "value" and "object" are
          synonymous.
          So far so good.
          So if you then insist that Python uses "call by object", you're actually
          saying it uses call by value!
          Fail!

          The terms "guy" and "man" are synonymous, but a wise guy and a wise man
          are not.



          --
          Steven

          Comment

          • Joe Strout

            Re: Finding the instance reference of an object [long and probablyboring]

            On Nov 7, 2008, at 6:21 PM, Aaron Brady wrote:
            Therefore objects don't need names to exist. Having a name is
            sufficient but not necessary to exist. Being in a container is
            neither necessary -nor- sufficient.
            What do you mean? Being in a container isn't necessary, but it
            certainly is sufficient.

            Having ANY references to it is both necessary and sufficient to
            exist. And isn't that the easiest way to say it?

            Best,
            - Joe

            Comment

            • Aaron Brady

              Re: Finding the instance reference of an object [long and probablyboring]

              On Nov 8, 8:42 am, Joe Strout <j...@strout.ne twrote:
              On Nov 7, 2008, at 6:21 PM, Aaron Brady wrote:
              >
              Therefore objects don't need names to exist.  Having a name is
              sufficient but not necessary to exist.  Being in a container is
              neither necessary -nor- sufficient.
              >
              What do you mean?  Being in a container isn't necessary, but it  
              certainly is sufficient.
              >
              Having ANY references to it is both necessary and sufficient to  
              exist.  And isn't that the easiest way to say it?
              No, you forgot about cyclically-linked garbage, which Python
              implementations are allowed to, but not required to, collect.

              Comment

              • Aaron Brady

                Re: Finding the instance reference of an object

                On Nov 8, 1:08 am, Steven D'Aprano <st...@REMOVE-THIS-
                cybersource.com .auwrote:
                On Sat, 08 Nov 2008 18:31:47 +1300, greg wrote:
                <Quote in favor of Steven snip>

                <Example in favor of Steven snip>
                There's no "obviously" about it. To anyone who has learned that "call-by-
                value" means that a copy is made, "obviously" it does mean copying the
                value. If you have learned a different meaning, then you will believe
                differently.
                I don't think it's obvious to everyone what a copy constructor is and
                when it's called. That's ok, it's something you can learn. I think
                Joe's idea is that you can think of every variable in Python as a
                pointer, and that clears up some confusions about its variable model.
                What happens when a pointer is copied? What is an example of copying
                a pointer in spoken language?
                >>a= [ 1, 2, 3 ]
                Are the following true?

                The value of 'a' is an object.
                The value of that object is [ 1, 2, 3 ].
                The value of 'a' is [ 1, 2, 3 ].

                If so, 'value' is ambiguous and therefore not very useful as a term.

                Comment

                • Terry Reedy

                  Re: Finding the instance reference of an object [long and probablyboring]

                  Steven D'Aprano wrote:
                  In an attempt to keep this post from hitting the ridiculous length of one
                  (Aside: I've learned one thing in this discussion. Despite the number of
                  sources I've read that claim that if you pass an array to a C function
                  the entire array will be copied, this does not appear to be true....)
                  Since C does not have an array type, it is impossible to pass an array.
                  int *a, b[10] declares *both* a and b as int pointers. As I remember,
                  the only difference is that b is initialized to the address of an
                  allocated block of 10. In expressions, b is an int pointer, just like
                  a, and a[i] and b[i] are defined the same, as *(a+i) and *(b+i), where
                  the addition is pointer arithmetic. Function definitions can only define
                  parameters as pointers (and lots else), not arrays. So passing either a
                  or b passes the address it represents. (Function names also represent
                  addresses in expressions, whereas, I believe, in C99 at least, struct
                  names represent the struc, not its address.)

                  tjr

                  Comment

                  • Grant Edwards

                    Re: Finding the instance reference of an object [long and probably boring]

                    On 2008-11-08, Terry Reedy <tjreedy@udel.e duwrote:
                    Steven D'Aprano wrote:
                    >In an attempt to keep this post from hitting the ridiculous length of one
                    >
                    >(Aside: I've learned one thing in this discussion. Despite the number of
                    >sources I've read that claim that if you pass an array to a C function
                    >the entire array will be copied, this does not appear to be true....)
                    >
                    Since C does not have an array type, it is impossible to pass an array.
                    int *a, b[10] declares *both* a and b as int pointers. As I remember,
                    the only difference is that b is initialized to the address of an
                    allocated block of 10.
                    No, that's not true. "b" is not a pointer that's initialized
                    with an address. "b" is a constant literal that is equivalent
                    to the address of the first element in the "array". IOW,
                    (void*)b == (void*)&b == (void*)&b[0].

                    That's not true for a. (void*)a == (void*)&a[0],
                    but (void*)a != (void*)&a.
                    In expressions, b is an int pointer, just like a, and a[i] and
                    b[i] are defined the same, as *(a+i) and *(b+i), where the
                    addition is pointer arithmetic.
                    True, but &a and &b are very different, and "a" can be used as
                    an lvalue and "b" can not. "a" is a variable, and "b" is a
                    literal constant (that can, for static storage classes, be
                    resolved to a value at compile/load time).
                    Function definitions can only define parameters as pointers
                    (and lots else), not arrays. So passing either a or b passes
                    the address it represents. (Function names also represent
                    addresses in expressions, whereas, I believe, in C99 at least,
                    struct names represent the struc, not its address.)
                    --
                    Grant

                    Comment

                    • Aaron Brady

                      Re: Finding the instance reference of an object

                      On Nov 8, 10:59 pm, Joe Strout <j...@strout.ne twrote:
                      Of course, I've softened my position somewhat, since being shown that  
                      "call by sharing" is simply a term for call-by-value in the case where  
                      the values are object references.  That clearly does apply to Python  
                      (as well as other OOP languages, where object references are  
                      involved), and as long as we can explain it as just a restricted form  
                      of call-by-value, I'm OK with that.
                      Humor me briefly. In 'x= [1, 2, 3]', do you think that the value of x
                      is a memory address?

                      Comment

                      • greg

                        Re: Finding the instance reference of an object

                        Steven D'Aprano wrote:
                        Not according to my Comp Sci lecturers back in the day, and not according
                        to my Pascal books.
                        Pascal books will tell you what call-by-value means
                        in Pascal. You can't just blindly take that description
                        and apply it to other languages, just as you can't
                        take what your Pascal book says about assigment and
                        apply it blindly to Python without getting into
                        trouble.

                        Describing call-by-value as "copying the value" works
                        in Pascal, because "assignment " and "copy" are synonyms
                        in that language. But they're not synonyms in all
                        languages, so a language-independent definition of
                        call-by-value needs to describe it in a more general
                        way.

                        Refusing to accept that a more general definition
                        exists just because it's not mentioned in your Pascal
                        book is a little absurd.

                        --
                        Greg

                        Comment

                        • greg

                          Re: Finding the instance reference of an object

                          Arnaud Delobelle wrote:
                          What's a variable reference?
                          It's a reference to a variable. It's what gets passed behind
                          the scenes when you use a VAR parameter in Pascal, or a
                          ByRef parameter in VB.
                          What you're saying is that in the code below, when foo(q) is called
                          then 'p' in foo is another name for q in main. Right?
                          >
                          struct point {
                          int x;
                          int y;
                          }
                          >
                          int foo(point p) {
                          p.x = 42;
                          }
                          >
                          int main() {
                          point q = {0, 0};
                          foo(q);
                          /* So now you're saying that q.x == 0 ? */
                          }
                          No. Passing q by value means that the value of the expression 'q',
                          whatever that is in the language concerned, gets assigned to the
                          local variable 'p', whatever *that* means in the language concerned.

                          Because of the way C assignment works, the result is that p ends
                          up with a copy of the whole struct.

                          Because of the way Python assignment works, the result is that
                          p and q end up referring to the same object.

                          The difference is *entirely* due to the difference in the semantics
                          of assignment between the two languages. Once you've taken that
                          into account, there is no need to look for difference in the
                          parameter passing scheme.

                          --
                          Greg

                          Comment

                          • greg

                            Re: Finding the instance reference of an object

                            Dennis Lee Bieber wrote:
                            You must have missed all the threads about "binding" then... Wherein
                            a Python "assignment statement" "binds the LHS name to the RHS object"
                            rather than "assigns the RHS object value to the LHS"
                            I know that it is sometimes referred to that way. But
                            nobody seems to get worked up into a religious fervour
                            and insist that the word "assignment " be banned from the
                            Python terminology lexicon, the way some people seem to
                            do about words such as "value" and "reference" .

                            If we can all get along nicely and accept that "assignment "
                            is an acceptable term, among alternatives, for something that
                            has an analogy, if not an exact equivalent, in other languages,
                            why can't we do the same for "reference" and "call by value"?

                            --
                            Greg

                            Comment

                            • Arnaud Delobelle

                              Re: Finding the instance reference of an object

                              greg <greg@cosc.cant erbury.ac.nzwri tes:
                              Arnaud Delobelle wrote:
                              >
                              >What's a variable reference?
                              >
                              It's a reference to a variable. It's what gets passed behind
                              the scenes when you use a VAR parameter in Pascal, or a
                              ByRef parameter in VB.
                              Do you mean you can't do the following C++ snippet in Pascal or VB? I
                              haven't used Pascal for more than 20 year and I have never used VB, so
                              this is a real question.

                              foo(int &x) {
                              x = 7;
                              }

                              struct bar {
                              int i;
                              float j;
                              };

                              int main() {
                              int a[10];
                              bar b;
                              // What is passed to foo below is obviously not a 'variable
                              // reference' as the argument is not a variable.
                              foo(a[3]); // Now a[3] == 7
                              foo(b.i); // Now b.i == 7
                              }

                              [...]
                              Passing q by value means that the value of the expression 'q',
                              whatever that is in the language concerned, gets assigned to the
                              local variable 'p', whatever *that* means in the language concerned.
                              >
                              Because of the way C assignment works, the result is that p ends
                              up with a copy of the whole struct.
                              >
                              Because of the way Python assignment works, the result is that
                              p and q end up referring to the same object.
                              >
                              The difference is *entirely* due to the difference in the semantics
                              of assignment between the two languages. Once you've taken that
                              into account, there is no need to look for difference in the
                              parameter passing scheme.
                              I'm not sure that your definition of 'call by value' is widely
                              accepted. If it was, then this thread wouldn't exist.

                              I've tried to google for an authoritative definition but it's harder
                              than I thought. This definition comes up everywhere though.

                              call-by-value

                              (CBV) An evaluation strategy where arguments are evaluated before
                              the function or procedure is entered. Only the values of the
                              arguments are passed and changes to the arguments within the called
                              procedure have no effect on the actual arguments as seen by the
                              caller.

                              [This from http://dictionary.reference.com/browse/call-by-value]

                              It seems to me that it more or less is the definition that most people
                              on this thread has been implicitly using.

                              --
                              Arnaud

                              Comment

                              • Marc 'BlackJack' Rintsch

                                Re: Finding the instance reference of an object

                                On Sun, 09 Nov 2008 11:17:28 +0000, Arnaud Delobelle wrote:
                                greg <greg@cosc.cant erbury.ac.nzwri tes:
                                >
                                >Arnaud Delobelle wrote:
                                >>
                                >>What's a variable reference?
                                >>
                                >It's a reference to a variable. It's what gets passed behind the scenes
                                >when you use a VAR parameter in Pascal, or a ByRef parameter in VB.
                                >
                                Do you mean you can't do the following C++ snippet in Pascal or VB? I
                                haven't used Pascal for more than 20 year and I have never used VB, so
                                this is a real question.
                                >
                                foo(int &x) {
                                x = 7;
                                }
                                >
                                struct bar {
                                int i;
                                float j;
                                };
                                >
                                int main() {
                                int a[10];
                                bar b;
                                // What is passed to foo below is obviously not a 'variable
                                // reference' as the argument is not a variable.
                                foo(a[3]); // Now a[3] == 7
                                foo(b.i); // Now b.i == 7
                                }
                                Translated to Pascal:

                                Program Test;

                                Type
                                Bar = Record
                                i: Integer;
                                j: Real;
                                End;

                                Var a: Array[0..9] Of Integer;
                                b: Bar;

                                Procedure Foo(Var x:Integer);
                                Begin
                                x := 7;
                                End;

                                Begin
                                Foo(a[3]);
                                WriteLn(a[3]); {Prints 7.}
                                Foo(b.i);
                                WriteLn(b.i); {Prints 7.}
                                End.

                                In "reality" I would not expect that anything is passed here but that the
                                compiler inlines it as direct assignment. Should read: Copying the bit
                                pattern of a 7 into the fixed memory locations. :-)

                                Ciao,
                                Marc 'BlackJack' Rintsch

                                Comment

                                Working...