A question on modification of a list via a function invocation

Antoon Pardon antoon.pardon at vub.be
Mon Sep 4 06:02:12 EDT 2017


Op 04-09-17 om 00:44 schreef Dennis Lee Bieber:
> 	And is a limited theoretical study, heavy in mathematics and light in
> implementation.
>
> 	Programming Languages: Design and Implementation (Terrence W Pratt,
> 1975, Prentice-Hall) has a whole section (6-9 Subprograms with Parameters:
> Parameter Transmission Techniques)... 
>
> """
> Basic parameter Transmission Techniques
>
> Transmission by Value:  ... the actual parameter is evaluated at the point
> of call. The /values/ of the actual parameter is then transmitted to the
> subprogram and becomes the initial value associated with the corresponding
> formal parameter. ...
>
> Transmission by Reference (Location or Simple Name): In transmission by
> reference a pointer is transmitted, usually a pointer to a data location
> containing the value. ... Any assignment to Y in SUB will change the value
> of X back in the calling program.

IMO that depends on the semantics of the assignment statement. In an environment
where an assignment copies the value into the object the variable points to, this
is correct. However if assignment provides a new object that is now bound to the
variable, it is incorrect.

The diagram below tries to illustrate the two different assignment semantics:

BEFORE
                         +-----+       +-----+
                         |     |       |     |
                         |  5  |       |  7  |
                         |     |       |     |
                         +-----+       +-----+

                            ^             ^
                            |             |
                           <x>           <y>


                                 x = y
AFTER

            C style                |         Python style
                                   |
     +-----+       +-----+         |                  +-----+
     |     |       |     |         |                  |     |
     |  7  |       |  7  |         |                  |  7  |
     |     |       |     |         |            --->  |     |
     +-----+       +-----+         |           /      +-----+
                                              /
        ^             ^                      /           ^
        |             |                     /            |
       <x>           <y>                  <x>           <y>


-- 
Antoon Pardon.




More information about the Python-list mailing list