A question on modification of a list via a function invocation

Larry Hudson orgnut at yahoo.com
Thu Aug 17 03:18:09 EDT 2017


On 08/16/2017 03:39 PM, Chris Angelico wrote:
> On Thu, Aug 17, 2017 at 8:29 AM, Mok-Kong Shen
> <mok-kong.shen at t-online.de> wrote:
>> I have earlier learned some other (older) programming languages. For
>> these the formal parameters are either "by reference" or "by value".
>> In the first case, any modification of the formal parameter inside
>> a function affects the corresponding actual parameter of a function
>> call, while in the second case a copy of the actual parameter is
>> passed into the function so that any modification of the formal
>> parameter inside the function has no effect at all outside. This is
>> extremely clear-cut in comparison to Python, isn't it? Anyway, while
>> any new user of a programming language certainly can be expected to
>> take good efforts to learn a lot of new stuffs, I suppose it's good
>> for any practical programming language to minimize the cases of
>> surprises for those that come from other programming languages.
> 
> Python has a data model that is neither of the above, but it's simpler
> in that you have one pattern for everything. Whether you're looking at
> function parameters, return values, assignment, loops, function
> definitions, or anything else, the model is exactly the same. And that
> model is: objects exist independently of names, and names refer to
> objects. If you do "x = y", you're saying "figure out which object 'y'
> means, and make the name 'x' refer to it". If you do "x[1] = y",
> you're saying "figure out which object 'y' means, and tell the object
> that 'x' means that it should make [1] refer to that object". So if
> you have multiple names referring to the same object, any change you
> ask that object to do will be seen by every other name that also
> refers to it - because it's all about the object.
> 
> ChrisA
> 

Here's another explanation, same concepts just stated differently, perhaps giving a different 
perspective.

In a "traditional" programming language, variables are created/assigned by:

Setting aside a chunk of memory to hold the data and associating the variable name with that 
memory.  And since this chunk is a fixed size, the data type that can be stored there is also 
fixed at that time as well.  This is static typing.

Assignment is done by storing the new data in that memory location, overwriting what was already 
there.  But it has to be the proper data type so it doesn't overrun the memory allocated for it.

Python does it in the opposite order:

The data is stored in memory (or already exists) and the variable name is associated with that 
memory.  Python calls this name binding.  It is possible, and common, that this memory (data) 
has multiple names bound to it.

Assignment is done by associating the name to a different data value (memory location).  This 
data may be newly created or it can be previously existing data.  (The old data may or may not 
be 'garbage collected' — depending on other factors.)  A key point here is that the name has no 
memory size/data type itself and can be bound to ANY data — this is dynamic typing.

Where things get confusing is when we consider mutable/immutable data.

If it is immutable (unchangeable:  ints, floats, strings, tuples...) the _practical_ result is 
that although the implementation details are different, the result of running the program is the 
same.

But for mutable data (changeable IN PLACE:  lists, dictionaries...) the results can be 
surprising from the viewpoint of other languages.  Since this (mutable) data can have multiple 
variable names bound to it, changing the value(s) through ANY of the names changes this data for 
ALL of the names bound to it.

The OP said...

 >>                                                 I suppose it's good
 >> for any practical programming language to minimize the cases of
 >> surprises for those that come from other programming languages.

I see your point, but it's irrelevant.  Part of learning any new language is to learn where/how 
the new language handles things differently from your previous experience.  You MUST learn the 
new ways and forget (or at least modify) your previous knowledge.  It's simply a necessary part 
of learning a new language.  Expect it and live with it!

I hope this description gives you another viewpoint to consider.  Good luck on your journey to 
becoming a Pythonista!

<For any pedants out there, my description here is meant strictly to describe these underlying 
concepts in general terms.  The details may be (and probably are) quite different.>

-- 
      -=- Larry -=-



More information about the Python-list mailing list