Python - how does passing values work?

后端 未结 1 1801
抹茶落季
抹茶落季 2020-11-30 11:28

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 s

相关标签:
1条回答
  • 2020-11-30 11:51

    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.

    0 讨论(0)
提交回复
热议问题