Suppose I have two Python functions, Function 1 and Function 2.
Function 1 will call Function 2 and the parameter is a big data (e.g., a dictionary with 100 thousand elements).
I am wondering if there is any performance differences between calling Function 2 in Function 1, which means I need to pass the big data parameter, and implementing Function 2 in Function 1 directly, which means I don't need to pass the big data parameter.
Thanks.
PS: I think the key question is how Python pass the parameter, by value, or by reference (pointer)?
Edit: It seems that this is a confused problem. How do I pass a variable by reference? is a good answer.
A function never makes those statements themselves slower. A Python function is just an object stored in a variable; you can assign functions to a different variable, replace them with something completely different, or delete them at any time.
Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables. In fact, you can think about a method (or function) as a variable whose value is the actual callable code object.
Python function arguments pass by reference or valueThe parameters in the python language are passed by reference. Which mean if we change what parameter refers to within the function, the change also reflect black in the calling function.
Python passes references-to-objects by value. The terminology is controversial and ugly, but there should be no real performance difference.
Check out these answers for all the details you could ever want (hopefully).
The terminology of how python "passes" is a vicious debate I don't want to go into. But what is actually passed on the stack is a reference. So there is not a large memory cost with either of your options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With