Saturday, May 21, 2005

N = N + 1

When first learning about programming, most people get confused by expressions likeN = N + 1The origin of the confusion is easy to understand. Expressions likeN = N + 1 often occurs in a computer program dealing with mathematical concepts. And, in mathematics, the expressionN = N + 1is just plain wrong. Ergo the confusion. To get around the confusion, some programming language introduce a different symbol for "variable assignment". Something like ":=" , or "

1. A little story

John, Pierre and Antonio are three good friends that share a house. John is originally from England, Pierre from France and Antonio from Spain.

One day, John was speaking in English to Pierre. During that conversation, John said the word "once", referring to an event that occurred a single time. Pierre translated this concept in his mind as "une_fois".

The various words in human languages are associated with concepts or objects that are independent of the words used to represent them. So, "une_fois" is associated with the number 1, through its association with the word "once". However, "once" is just a word, not an object. So, when "once" takes on a different value later, the concept represented by "une_fois" is unchanged.

2. The meaning of = in Python

In Python the symbol "=" associates a synonym on its left hand side to a concept on its right hand side.synonym = concept/object

Words that are chosen as synonyms, are chosen in a context dependent way. In our later story above, names are chosen based on the "human language context". The same combination of letters can mean two different things in different languages, or even in the same language. I remember a little test a friend of mine did in grad school, showing parts of a newspaper headline to fellow classmates (all science geeks) and asking them to quickly read it aloud. The word "unionized" appeared in the headline. Now, think "labour" and "chemistry" and you will think of two different way of pronouncing this word.

Now, in the typical programming situation, "N+1" represents a mathematical concept. We can give it a (temporary) synonym of our own chosing, depending on the context...3. Another short story

John makes a groceries shopping list, with two items: "bananas" and "pears". He then tapes it on the fridge door.

Later that day, Pierre walks by and notices the list too (une liste d'épicerie) which he associates with the word epicerie. Still later that day, John adds "apples" to the list on the fridge door. He notices that Antonio had already bought pears and remembers that they also need oranges. He thus scratches the item "pears" and replace it by "oranges".

The list has changed throughout the day, but it remains taped to the fridge. When they think about the grocery list, both John and Pierre are referring to the same object in their own language. When that object changes, it changes for both of them in the same way.

4. Yet another short story

John makes a groceries shopping list, with two items: "bananas" and "pears". He then tapes it on the fridge door. Later that day, Pierre walks by and notices the list and decides to make his own copy.

>>> groceries = ["bananas", "pears"]>>> epicerie = list(groceries)

Still later that day, John adds "apples" to the list on the fridge door. He notices that Antonio had already bought pears and remembers that they also need oranges. He thus scratches the item "pears" and replace it by "oranges".

Regarding the confusion that I created, according to the last comment (and taking it at face value):

The reality is that for *many* people introduced to computer programming, statements like N=N+1 are confusing. If you read about computer language design and pedagogy, you will find that the problem of using "=" as an assignment operator is often noted. This is why, as I mentioned in the article, some languages use a different symbol (like :=, or a left-arrow) to denote variable assignment.

I suspect that most people that become successful programmer are those who, like you, were not confused by this. I was never confused by it myself (from way back to the old Fortran days ;-) but have since read about this problem faced by many.

I'm not a Python programmer (actually not a programmer by profession, more of a systems person) so perhaps you can confirm what I'm getting from this article.

Scalar assignments (like:une_fois = once) are assignments by value ('une_fois' takes the value that 'once' has at the time of the assignment and remains unaffected by subsequent assignments to 'once'?

List assignments (like:epicerie = groceries) are assignments by reference ('epicerie' as a list changes value when 'groceries' as a list changes value)?

Or should I be thinking of list variables in Python as references to lists, thus the assignment:epicerie = groceriesis really copying the value of groceries, which is a reference to a list, thus when the list is changed by accessing it through 'groceries' reference, the same list is seen to be changed when accessing it through the 'epicerie' reference?

First: I am just a hobbyist myself - so take my assertions with a grain of salt :-)

In Python, *everything* is an object.[As an experiment, try dir(a) to see the methods associated with instance or object "a". For example, dir(1).]

Whereas other language make a difference between assignments "by value" or "by reference", in Python there is essentially (afaik) only one type of assignment.

On the right hand side of the = sign is an object. On the left hand side is a "reference" to that object. I prefer to use the word "synonym" rather than "reference", to try and break the link between what you have learned from other languages and think of Python by itself.

So, in the first example, "once" is a synonym given to object "1". A further assignement like une_fois = onceis equivalent toune_fois = 1where the "synonym" 'once' has been replaced on the right hand side by the object it "refers" to.

It does not matter whether the object on the right hand side is a scalar, a list or some more complicated object.

Note that '1' is an object that can not change (all numbers are like that).

In the second example, "groceries" and "epicerie" are two 'synonyms' that refer to the same object. Whether you manipulate this object (change its content, since it is not a scalar) through one or the other synonym does not alter the fact that there is only one object that is changed. That object (a list) is a mere container for other objects.

You could take the second example, and add at the endgroceries = 3.Now, "groceries" has been given another meaning and only "epicerie" remains as a synonym to the list.

No, I don't agree with you!the assignmenet a=b is the same whatever is b.In python all asignements are reference.a is a new name for the object (it now has 2 names a and b).But there are some object that are mutable (like list, dict ..) and othere that are not.so when you did a=1, you could chnage the inner value of 1 (int are not mutable)!A list is mutable, a=[], you can do a.append(1)...

When you did a=b this is the same object (you can see it with "a is b") whatever the type of object.Every time it is a reference assignement, just mean mutable / not mutable object !

Morever you write:'une_fois' takes the value that 'once' has at the time of the assignment and remains unaffected by subsequent assignments to 'once'?

It is exacly the same for list !a=[]b=aa.append(1)a=[]b is [1] and a is []! so no affectation of the value of b by assignement of a.

I hope it will clear everithing. But it is really important to undersand python as name for object (no assignement like in other language) as you can see withn the NameError exception !

For the record, it is "Bruce" that said that Scalar assignments ... are assigned by value. ...

I agree with your statement that the assignmenet a=b is the same whatever is b.In python all asignements are reference.

In fact, I wrote, in reply to Bruce:It does not matter whether the object on the right hand side is a scalar, a list or some more complicated object....On the right hand side of the = sign is an object.On the left hand side is a "reference" to that object...

Note that I tried to explain things in the simplest possible way (without even touching concepts like mutability), as if I were trying to explain what "=" means to someone who has never done any programming before. Perhaps I oversimplified?