python pandas dataframe, is it pass-by-value or pass-by-reference

后端 未结 6 880
陌清茗
陌清茗 2020-12-02 05:58

If I pass a dataframe to a function and modify it inside the function, is it pass-by-value or pass-by-reference?

I run the following code

a = pd.Data         


        
6条回答
  •  再見小時候
    2020-12-02 06:32

    Python is neither pass by value nor pass by reference. It is pass by assignment.

    Supporting reference, the Python FAQ: https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference

    IOW:

    1. If you pass an immutable value, changes to it do not change its value in the caller - because you are rebinding the name to a new object.
    2. If you pass a mutable value, changes made in the called function, also change the value in the caller, so long as you do not rebind that name to a new object. If you reassign the variable, creating a new object, that change and subsequent changes to the name are not seen in the caller.

    So if you pass a list, and change its 0th value, that change is seen in both the called and the caller. But if you reassign the list with a new list, this change is lost. But if you slice the list and replace that with a new list, that change is seen in both the called and the caller.

    EG:

    def change_it(list_):
        # This change would be seen in the caller if we left it alone
        list_[0] = 28
    
        # This change is also seen in the caller, and replaces the above
        # change
        list_[:] = [1, 2]
    
        # This change is not seen in the caller.
        # If this were pass by reference, this change too would be seen in
        # caller.
        list_ = [3, 4]
    
    thing = [10, 20]
    change_it(thing)
    # here, thing is [1, 2]
    

    If you're a C fan, you can think of this as passing a pointer by value - not a pointer to a pointer to a value, just a pointer to a value.

    HTH.

提交回复
热议问题