Python dictionary pass by value or by reference? What about pandas data frame?

A nice discussion and examples can be found in [1]. The quick answer from the top voted of [1] is:

Python’s “parameter evaluation strategy” acts a bit different than the languages you’re probably used to. Instead of having explicit call by value and call by reference semantics, python has call by sharing. You are essentially always passing the object itself, and the object’s mutability determines whether or not it can be modified. Lists and Dicts are mutable objects. Numbers, Strings, and Tuples are not.

You are passing the dictionary to the function, not a copy. Thus when you modify it, you are also modifying the original copy. From the top voted answer of [1]

An interesting example

def f(a_list):
a_list.append(1)

m = []
f(m)
print(m)

The output is [1]

However, for the following

def f(a_list):
a_list = a_list + [1]

m = []
f(m)
print(m)

The output is []. Detail of the reason please see [2]: “call by sharing”.

Pandas Data Frame

What about pandas dataframe? Sicne dataframe is mutable, it behaves similar to python list.

>>> import pandas as pd
>>> df = pd.DataFrame({"a": [1, 2, 3]})
>>> def modify_df(df):df["b"] = df["a"]
...
>>> df
a
0 1
1 2
2 3
>>> modify_df(df)
>>> df
a b
0 1 1
1 2 2
2 3 3

Reference

[1] https://stackoverflow.com/questions/15078519/python-dictionary-passed-as-an-input-to-a-function-acts-like-a-global-in-that-fu

[2] https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store