PYTHON is UNFAIR. Its treating list and a non list variable with partiality

Don’t think of variables in Python as named boxes in which you store data. Think of them as nametags you stick on objects.

In your function, you are sticking the name x on the object 8. This is a local name, so it goes away when the function ends. Inside the function, the local name x “shadows” (prevents access to) the global name x so the global name x is not affected by the assignment. It still points to the object 5 and when you print it outside the function, that’s what you get.

You are not changing what object the name mat refers to inside the function. Rather you are changing (mutating) the object pointed to. A method call, even one that changes the object, is not the same as an assignment. No local name mat is created so you are mutating the same object you created outside the function.

This is a common pitfall for beginning Python programmers, but it is understood easily enough and once understood, does not pose any significant difficulty in programming in the language.

The fact that you don’t understand something doesn’t make it “UNFAIR.” Python has been around for more than two decades. It was designed by very smart people and is used by, probably, tens of thousands of programmers around the world. If there were something wrong with Python at this fundamental of a level, it would have been fixed by now.

Browse More Popular Posts

Leave a Comment