Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - how does passing values work?

I have a question about function calls in Python. Say I want to write a function called superLongFunc(expr). The function is super long and really hard to debug. I want to split the function into smaller helper functions for better readability, to something like smallFunc1(expr),smallFunc2(expr), etc.

My question is, does this affect the performance of the code at all? How exactly does calling functions work in Python? Does Python pass the variables to a function by reference? Or does it make a copy of the variable before feeding it to the function?

I know it's a pretty nooby question but it's been bugging me for a while. Thanks in advance!

like image 415
turtlesoup Avatar asked Jul 20 '12 19:07

turtlesoup


1 Answers

Python uses a system sometimes called call-by-object. Nothing is copied when you pass arguments to a function. The names of the function arguments are locally bound within the function body, to the same objects provided in the function call.

This is different from what most people think of as "call by value", because it doesn't copy the objects. But it's also different from "call by reference" because the reference is to the object --- a new name is bound, but to the same object. This means that you can mutate the passed-in object, but rebinding the name inside the function has no effect outside the function. A simple example of the difference:

>>> def func(x):
...     x[0] = 2 # Mutating the object affects the object outside the function
>>> myList = [1]
>>> func(myList)
>>> myList # myList has changed
[2]
>>> def func(x):
...     x = 2 # rebinding name has no effect outside the function
>>> myList = [1]
>>> func(myList)
>>> myList # myList is unaffected
[1]

My simple way of thinking about this is that assignment to a bare name --- that is, statements of the form name = value --- is completely different from everything else in Python. The only way to operate on names and not on values is to do name = value. (There are nitpicky exceptions to this, like mucking around with globals() and so on, but these are dangerous territory anyway.) In particular name = value is different from obj.prop = value, obj[0] = value, obj += value, and other similar things that look like assignment but actually operate on objects and not on names.

That said, function calls in Python have a certain amount of overhead just in themselves (for setting up the execution frame, etc.). If a function is called many times, this overhead can cause a noticeable performance impact. So splitting one function into many could still have a performance impact, since each additional function call will add some overhead.

like image 145
BrenBarn Avatar answered Oct 19 '22 23:10

BrenBarn