Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Variable Scope (passing by reference or copy?)

Tags:

People also ask

Does Python pass by copy or reference?

Python passes arguments neither by reference nor by value, but by assignment.

Are Python variables passed by reference?

Python always uses pass-by-reference values. There isn't any exception. Any variable assignment means copying the reference value.

Is Python pass by value or pass-by-reference '?

The two most widely known and easy to understand approaches to parameter passing amongst programming languages are pass-by-reference and pass-by-value. Unfortunately, Python is “pass-by-object-reference”, of which it is often said: “Object references are passed by value.”

Is Python call by reference or value?

Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function.


Why does the variable L gets manipulated in the sorting(L) function call? In other languages, a copy of L would be passed through to sorting() as a copy so that any changes to x would not change the original variable?

def sorting(x):
    A = x #Passed by reference?
    A.sort() 

def testScope(): 
    L = [5,4,3,2,1]
    sorting(L) #Passed by reference?
    return L

>>> print testScope()

>>> [1, 2, 3, 4, 5]