learning python ...

Michael F. Stemper mstemper at gmail.com
Thu May 27 17:28:50 EDT 2021


On 27/05/2021 16.13, Christian Gollwitzer wrote:
> Am 25.05.21 um 06:08 schrieb hw:
>> On 5/25/21 12:37 AM, Greg Ewing wrote:
>>> Python does have references to *objects*. All objects live on
>>> the heap and are kept alive as long as there is at least one
>>> reference to them.
>>>
>>> If you rebind a name, and it held the last reference to an
>>> object, there is no way to get that object back.
>>
>> Are all names references?  When I pass a name as a parameter to a 
>> function, does the object the name is referring to, when altered by 
>> the function, still appear altered after the function has returned?  I 
>> wouldn't expect that ...

> The strange thing, coming from a different language, is the apparent 
> difference, if instead of a list, you pass an integer:
> 
> def f(a):
>      a=5
> 
> l=3
> 
> f(l)
> print(l)
> 
> ====> 3
> 
> Here, the "l" is not changed. The reason is that the statement "a=5" 
> does NOT modify the object in a, but instead creates a new one and binds 
> it to a. l still points to the old one. Whereas a.append() tells the 
> object pointed to by a to change.

What was fun in Fortran was that you could do the following:

       CALL SS(3)
       PRINT *, 3
       END
       SUBROUTINE SS(K)
       K = 5
       RETURN
       END

The output of this gem would be 5, at least though Fortran 77. It was
call by reference, even for constants. (Every manual said not to do
this, of course.)

-- 
Michael F. Stemper
Outside of a dog, a book is man's best friend.
Inside of a dog, it's too dark to read.


More information about the Python-list mailing list