How does python determine if a parameter is a reference or a value?

In C ++, void somefunction(int) passes a value, and void somefunction(int&) passes a link. In Java, primitives are passed by value, and objects are passed by reference. How does python make this decision?

Edit: since everything is passed by reference, why is this:

 def foo(num): num *= 2 a = 4 foo(a) print(a) 

type '4' instead of '8'?

+4
source share
6 answers

There is disagreement on terminology. In the Java community, they say that everything is passed by value: primitives are passed by value; links are passed by value. (Just find this site for Java and link if you do not believe it.) Note that "objects" are not values ​​in the language; only links to objects.

The difference that they use is that in Java, when you pass a link, the original reference variable in the caller area can never be changed (i.e. made to point to another object) as the caller, which should be possible in the link pass. Only the object referenced can be mutated, but it does not matter.

Python values ​​work just like Java references. If we use the same definition, we will say that everything in Python is a reference, and everything is passed by value. Of course, some of the Python community use a different definition.

Disagreement over terminology is the source of much of the confusion.

Since you mention C ++, the Python code you have will be equivalent to something similar in C ++:

 void foo(const int *num) { num = new int(*num * 2); } const int *a = new int(4); foo(a); print(a); 

Note that the argument is the pointer that most closely resembles references in Java and Python.

+9
source

It passes everything by reference. Even when you specify a numeric value, this is a link to a table containing that value. This is the difference between static and dynamic languages. The type remains with the value, not the container, and the variables are just references to the "value space" where all the values ​​live. You can accept this value space containing all possible immutable objects (integers, floats, strings), as well as all objects you modify (lists, dictations, objects). Of course, their existence becomes concrete only when you use them (this means that if you never use number 42 in your program, there is no allocated space for value 42 in the "value space")

He does this because the number to which he refers is an immutable object. 4 - 4 no matter what.

 def foo(num): # here, num is referring to the immutable entity 4 num *= 2 # num now refers to the immutable entity 8 a = 4 # a now is pointing to the immutable entity 4 foo(a) # a is still referring to the same entity 4 print(a) # prints what a refers to, still 4 

However, if you do this

 def foo(l): # here, l refers to the list it receives l.append(5) # the list is appended with the number 5 a = [] # a now is pointing to a specific mutable list foo(a) # a is still referring to the same specific mutable list print(a) # prints what a refers to, the specific mutable list which now contains [5] 
+11
source

In response to your editing, this is because integers are immutable in Python. So a does not change for the same reason that it does not change when this code is run:

 a = 4 num = a num *= 2 print(a) 

You do not change num (and therefore a ) in place, you create a new number and assign it num.

+3
source

Arguments are actually passed by value. The function is passed to the object to which the variable belongs, and not to the variable itself. Function cannot recheck caller variables. A function cannot change an immutable object, but it can change (request changes) mutable.

+3
source

Everything is passed by reference. Everything is also an object.

+1
source

This is not about the semantics of the function call, but the semantics of the destination. Python assignment is done by reordering the link, not by overwriting the original object. This is why the sample code prints 4 instead of 8 - it has nothing to do with the variability of objects as such, moreover, the * = operator is not a mutator, but a multiplication followed by an assignment. Here num * = 2 essentially binds the name num 'in this function to a new object of value num * 2 . The original value you passed in remains unchanged.

+1
source

All Articles