News for hackers. Stuff that matters.
Re:Reasons Python Sucks by colanderman on Tuesday December 18, 2018 @09:36AM (#18706601)
Python (and Java) do not pass anything by reference. They pass by pointer-value. (If they passed by reference, you could change to which value a caller's variable was bound, like you can in C++).
Re:Reasons Python Sucks by lmm on Tuesday December 18, 2018 @10:54AM (#18707416)
Those languages use references rather than pointers (that is, their referring-things do not support arithmetic), so any name for what they do that involves "pointer" is a poor one.

The distinction between passing the value of a reference being used by the caller and passing a reference to the caller's stack is so rarely important or useful that there's no consensus terminology for it. The distinction that's actually relevant and useful is whether, when we write f(a), f receives the (thing we would call the) value of a or a reference to the value of a (and in particular whether f can change the value of a). In Python, f can change the value of a (that is to say, the thing Python programmers understand to be the value of a); therefore Python is pass by reference as the term is usually understood (and certainly any term for its call style that uses "value" is more misleading than calling it "pass by reference").

Re:Reasons Python Sucks by jerf on Tuesday December 18, 2018 @11:31AM (#18707860)
"The distinction between passing the value of a reference being used by the caller and passing a reference to the caller's stack is so rarely important or useful that there's no consensus terminology for it."

And the reason for that is essentially all modern languages make copies of something when passing argument parameters. The only question is what they are passing and exactly how hard the language layer works to make it look as if you are or are not passing something by reference.

I have to reach to something as obscure as Forth to find a language that does not work that way, and truly does not copy parameters into a function. (I haven't dug into Factor enough to know if under the hood it still copies parameters.) It's a very unusual choice to not copy something for a function call.

(This is one of the ways our CPUs have been optimized for the languages we run; they make this intuitively expensive operation otherwise cheaper than it would be on a naively designed CPU.)

Re:Reasons Python Sucks by com2kid on Tuesday December 18, 2018 @12:39PM (#18708584)
> And the reason for that is essentially all modern languages make copies of something when passing argument parameters.

Then of course someone turns on whole program optimization for C++, half the code gets inlined into one terrifying large function, nothing is copied anywhere, and the code runs 2x faster.

Then some poor SOB has to debug the assembly code for all of this and has nightmares for years after.

Not like I'd know or anything. :)`

Re:Reasons Python Sucks by colanderman on Wednesday December 19, 2018 @09:30AM (#18715804)
"Pass by value" and "pass by reference" are both wrong with respect to Python. They denote semantics that Python does not have, and coders believe and do wrong things if they believe Python follows one or the other. Hence a 3rd term is needed. I don't care what you call it, because it's just a name, but it is a distinct idea, and I'm not the only one who thinks this: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sh...

> In Python, f can change the value of a

but it can't. `f` can't change the object which `a` references:

  def f(x): x = 5
is a function with no visible effect. When called as `f(a)`, `a` retains whatever value it had previously.

People who believe Python is "call-by-reference" don't understand this and write incorrect or overcomplicated code as a consequence. The distinction is important.

Re:Reasons Python Sucks by lmm on Wednesday December 19, 2018 @09:56AM (#18715959)
> They denote semantics that Python does not have

There is no consensus that "pass by reference" denotes that, whatever a wikipedia editor says. Normal working programmers do not understand "pass by reference" to mean specifically "pass a reference to the caller's stack" but only the more general concept of "pass a reference to the value of a". If you want to talk specifically about what kind of reference gets passed, you need some new terms for that, and both the new terms you come up with will be subtypes of what most of the world will continue to understand as a general category of "pass by reference". No-one outside of these arguments on HN gets confused about whether you can write a swap function in Python; people understand "pass by reference" to mean something more general and entirely true of Python (and Java and so on). You can tell by how often we see those languages described as "pass by reference".

> but it can't. `f` can't change the object which `a` references:

Yes it can. It can't make a to refer to a different object (as your f tries to), but it can change the object a refers to just fine.

Re:Reasons Python Sucks by alexhutcheson on Tuesday December 18, 2018 @09:57AM (#18706776)
> (If they passed by reference, you could change to which value a caller's variable was bound, like you can in C++)

I might be misunderstanding you, but I don't think you can do this in C++. References can't be changed to point to a different object after initialization. If you have code like:

  void MyFunc(Foo& ref_param) {
    Foo new_foo;
    ref_param = new_foo;
  }
The assignment above isn't "changing the value to which a caller's variable is bound". Instead, it's running the '=' operator on the Foo object to copy the state from new_foo to ref_param. To demonstrate this, you could run the following to see that the addresses are the same:

  Foo original_object;
  Foo& object_ref = original_object;
  MyFunc(object_ref);
  // object_ref still points to the same address
  assert(&original_object == &object_ref);
Under the hood, C++ reference params are basically syntactic sugar for passing by pointer-value, so this behavior isn't surprising.
Re:Reasons Python Sucks by colanderman on Tuesday December 18, 2018 @10:03AM (#18706832)
The proper analogy for a Python object is `std::shared_ptr<Foo>`: in Python, all "objects" are actually reference-counted pointers to objects (thus making them shareable). And then yes, in C++, you can change to which object the caller's "object" (pointer-to-object) points to, if you pass a reference to that. This is not possible in Python: all you can do is pass that shared-pointer value around, not the value of the object itself, nor a reference to the shared pointer.

This is a well-known distinction that goes by several names, see e.g. https://en.m.wikipedia.org/wiki/Evaluation_strategy#Call_by_...

Re:Reasons Python Sucks by alexhutcheson on Tuesday December 18, 2018 @10:09AM (#18706890)
Ah, got it. Your basic point is that C++ params can use two layers of indirection (pointer-to-ref, ref-to-pointer, pointer-to-pointer, etc.), while doing so in Python is clunky. Makes sense.
Re:Reasons Python Sucks by pdonis on Tuesday December 18, 2018 @11:50AM (#18708034)
> They pass by pointer-value.

No, Python doesn't even do that, because Python variables aren't names for storage locations to begin with. They're namespace bindings. Passing a variable to a Python function means transferring a namespace binding from the caller's namespace to the function's local namespace.

Re:Reasons Python Sucks by arduinomancer on Tuesday December 18, 2018 @01:04PM (#18708837)
What is a namespace binding? Can you explain at a low level what that means?
Re:Reasons Python Sucks by pdonis on Tuesday December 18, 2018 @05:38PM (#18711261)
Compare a variable assignment in C and Python.

In C:

  int i = 17;
Means "set aside one int's worth of storage and fill it with the bit pattern for the number 17".

In Python:

  i = 17
means "create an int object with the value 17 and bind it in the current namespace dictionary to the name i".
Re:Reasons Python Sucks by colanderman on Tuesday December 18, 2018 @04:05PM (#18710556)
The semantics are identical.
Re:Reasons Python Sucks by pdonis on Tuesday December 18, 2018 @05:38PM (#18711266)
If you think this, I'm confused about what you mean by "semantics".