[Edu-sig] How does Python do Pointers?

Michael H. Goldwasser goldwamh at slu.edu
Tue May 6 05:11:05 CEST 2008


Hi Brad,

  The problem I see with the "call-by-assignment" terminology is that
  it is ambiguous.  In fact, all parameter passing in Python, Java,
  and C++ would satisfy your call-by-assignment description.  The key
  is that the assignment semantics is different in different
  circumstances.

  To illustrate, consider a Java method with the following signature.

      public void doSomething(int x, Widget w) {  ... }

  If a caller invokes this function using a syntax such as

      foo.doSomething(myAge, myToaster)

  The information passing is precisely the same as assigning formal
  parameters to actual parameters as in

      x = myAge;
      w = myToaster;

  The difference in parameter passing models really boils down to the
  distinction between assignment semantics for the int type versus an
  Object type.

  In C++, the different forms of parameter passing also are all based
  on the underlying assignment semantics.  Calling a function with
  signature 

      void doSomething(int x, Widget w) {  ... }

  is essentially the same as if the first action within the body were
  the assignments

      int x = myAge;
      Widget w = myToaster;

  Notice that both of these are value variables and so we get a new
  int and a new Widget (techincally using the copy constructor rather
  than the assignment operator, but close enough).

  If we declare a signature such as

      void doSomething(int& x, Widget& w) {  ... }

  it would be akin to the declaration and assignments

      int& x = myAge;
      Widget& w = myToaster;

  Finally, if pointers are passed with a signature

      void doSomething(int* x, Widget* w) {  ... }

  and of course now the caller uses syntax doSomething(&myAge, &myToaster)
  to send pointers, we again get precisely the same semantics as the
  implicit assignments

      int* x = &myAge;
      Widget* w = &myToaster;


  So in all of these cases, as with Python, the key seems to be having
  a correct understanding of the assignment semantics.  I wish there
  were a better commonly accepted name for the parameter passing.
  Python's model is quite clean, it just doesn't match up perfectly
  with the standard call-by-??? terminology.


With regard,
Michael


On Monday May 5, 2008, Brad Miller wrote: 

>   Just to muddy the waters even further, David Ranum and I have adopted  
>   the terminology call by assignment for our upcoming CS1 book.  we  
>   found this terminology a few Python references and liked it.  Here's  
>   an excerpt:
>   
>   There are many different ways to pass parameters and different
>   programming languages have chosen to use a variety of them. In Python,  
>   however, all parameters
>   are passed using a single mechanism known as call by assignment  
>   parameter passing.
>   
>   Call by assignment parameter passing uses a simple two step process to  
>   pass data when the
>   function is called, also known as invocation. The first thing that  
>   happens is that the actual parame-
>   ters are evaluated. This evaluation results in an object reference to  
>   the result. In the first case from
>   Session 6.11, evaluating a literal number simply returns a reference  
>   to the number itself. In the
>   second example, evaluating a variable name returns the object  
>   reference named by that variable.
>   
>   Once the evaluation of the actual parameters is complete, the object  
>   references are passed to
>   and received by the formal parameters in the function. The formal  
>   parameter becomes a new
>   name for the reference that is passed. In a sense it is as if we  
>   executed the assignment statement
>   formal parameter = actual parameter.

       +-----------------------------------------------
       | Michael Goldwasser
       | Associate Professor
       | Dept. Mathematics and Computer Science
       | Saint Louis University
       | 220 North Grand Blvd.
       | St. Louis, MO 63103-2007




More information about the Edu-sig mailing list