A question on modification of a list via a function invocation

Rick Johnson rantingrickjohnson at gmail.com
Sat Aug 19 06:07:07 EDT 2017


On Wednesday, August 16, 2017 at 4:07:14 PM UTC-5, Mok-Kong Shen wrote:
> The above shows that with <assignment>, i.e. assigning
> single values to individual members of alist (with
> alist[0]=3 etc.) is "principally" different from assigning
> a whole list to alist (with alist=[30,60,90]). The first
> operation doesn't affect the connection between ss and
> alist, while the second separates the connection between ss
> and alist, as your diagram above clearly indicates.

And why is that so difficult for you to understand? It has
always seemed perfectly logical to me...

    [A thought experiment]
    Consider automobiles as an example. Automobiles are
    constructed in factories, and they are machines that are
    made of many subcomponents, but we reference the entire
    "conglomeration of subcomponents" (a "car") as single unit,
    and each unit is given a serial number (aka: "Vehicle
    Identification Number"). Now image one day your car is
    wrecked, and your mechanic replaces a few (or all) of the
    subcomponents with new subcomponents... would he also modify
    the VIN? Of course not! Because the subcomponents do not
    define the vehicle. If we want a new vehicle, then we
    request one from the factory.
    
So when you do this:

    car = [1,2,3]

You are requesting a _new_ car from the factory. But when
you do this:

    car[0] = 10
    car[1] = 20
    car[2] = 30
    ...
    
You are merely changing out subcomponents of an _existing_
car. And even if you exchange every component, the car will
never be a "factory new" car.
 
> Isn't this kind of convention/rule something that appears
> to be not quite natural/"logical" to the common users (non-
> experts)?

No.




More information about the Python-list mailing list