In python everything goes by reference
In Python, everything is a reference, and the references get passed around by value.
If you want to use those terms. But those terms make things harder to understand.
Much simpler: in Python, a variable is a name for an object. =
is used to change what object a name refers to. The left-hand side can refer to part of an existing object, in which case the whole object is changed by replacing that part. This is because the object, in turn, doesn't really contain its parts, but instead contains more names, which can be caused to start referring to different things.
then when is a new object created ?
Objects are created when they are created (by using the class constructor, or in the case of built-in types that have a literal representation, by typing out a literal). I don't understand how this is relevant to the rest of your question.
m = m[1:] # m changes its reference to the new sliced list
Yes, of course. Now m
refers to the result of evaluating m[1:]
.
edits m but not d (I wanted to change d)
Yes, of course. Why would it change d
? It wasn't some kind of magic, it was simply the result of evaluating d['m']
. Exactly the same thing happens on both lines.
Let's look at a simpler example.
m = 1
m = 2
Does this cause 1
to become 2
? No, of course not. Integers are immutable. But the same thing is happening: m
is caused to name one thing, and then to name another thing.
Or, another way: if "references" were to work the way you expect, then the line m = m[1:]
would be recursive. You're expecting it to mean "anywhere that you see m
, treat it as if it meant m[1:]
". Well, in that case, m[1:]
would actually mean m[1:][1:]
, which would then mean m[1:][1:][1:]
, etc.