There is no notion of variable binding in Python, that's a different thing. z, like any Python variable, is a reference to something. Initially, it's a reference to the same dictionary that x references. If we modify the object referenced by z (e.g. by adding a new item), we of course also modify the object referenced by x, as they are referencing the same object initially. However, when we assign something to z, we change the object that z is referencing. This has no effect on x, because x was passed-by-value to foo().
Pass-by-reference doesn't exist in Python. Here's what it looks like in C#, which does suport it:
auto x = Dictionary<string, int >() ;
x.Add("a", 1);
foo(ref x);
System.Println(x); //prints {b: 2}
void foo(ref Dictionary<string, int> z) {
auto k = new Dictionary<string, int>();
k.Add("b", 2);
z = k;
}
Here z is just a new name for x. Any change you make to z, including changing its value, applies directly to x itself, not just to the object referenced by x.
Pass-by-reference doesn't exist in Python. Here's what it looks like in C#, which does suport it:
Here z is just a new name for x. Any change you make to z, including changing its value, applies directly to x itself, not just to the object referenced by x.