Somehow I had the mantra "in #Python you use names, not variables" in my head but now I see that the tutorial uses the word "variable": docs.python.org/3/tutorial/int

I thought using "names" is what allowed Python to be so dynamic about what kind of values/types they refer to.

Am I lost with some bad terminology? Is there a canonical explanation about this?

Follow

@cartocalypse In Python, everything is an object and variables hold references to objects.

In Java, in contrast, a few things (primitive values like ints and chars) are NOT objects, so variables hold them directly.

The main consequence of a reference is the possibility of aliasing. If you

a = [1, 2, 3]
b = a
a[0] = 10

then b is [10, 2, 3].

The line b = a above makes a second reference to the list rather than copying the list, so modifying the list affects a and b.

Objects like Python ints and strings are immutable (can't be modified), so the programmer can largely ignore whether variables hold them directly or hold references to them. It is possible to tell whether two variables refer to the same object by using the "is" operator.

@peterdrake @cartocalypse I slightly disagree with the term „reference“ here. References allow the assignment of an new object:
a = Value
b = a
b = Other <- would also affect a
a == b <- in case of references

@spencer @cartocalypse Is there a language that does it that way? I would find it surprising.

There is the related notion of passing function parameters by reference, which (IIRC) C++ and C# allow. Python and Java pass by value; the passed value is always a reference in Python.

@peterdrake @cartocalypse IIRC C++ (non-const references, &) and VB (ByRef for procedure/function-params) behave like I described above.
This is impossible in with Python at all. <name> = <expression> simply binds the object resulting from the expression to the name. Objects bound before get unbound (and maybe garbage-collected).

@spencer @cartocalypse Oh, that's right -- but you have to declare b to be of type &int instead of int, right?

I have more experience with Java, which uses the word "reference" to mean "non-primitive" (and thus subject to aliasing), but doesn't allow this sort of thing.

@peterdrake @cartocalypse You‘re right. b must be declared as reference. With Java AFAIR POJOs are passed by value everything else by reference. This is why the final-keyword exists; to ensure the referenced object won‘t get replaced (by accident).

@spencer @cartocalypse I've heard the latter situation described as "passed by value, but the value is a reference".

The difference is that f(a) can modify the object to which a refers, but it can't cause a to point to a different object.

@peterdrake @cartocalypse Our discussion here pivots around the term „reference“. Well, it‘s a matter of definition what a reference is in different environments (programming languages). My prior knowledge of references with said languages was a real hurdle for me when starting with Python and trying to understand how these things behave. At some point it made click (clank, with stated binding-behavior). From then I avoid using the term „reference“ together with #Python

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.