News for hackers. Stuff that matters.
Re:Reasons Python Sucks by pkolaczk on Tuesday December 18, 2018 @11:54AM (#18708076)
7 Isn't Python actually pass-by-value / pass-pointer-to-object-by-value like Java is? Many confuse pass-pointer-to-object-by-value with pass-by-reference, but they are different things. Pass-by-reference allows to write a procedure that swaps two external values.
Re:Reasons Python Sucks by clumpthump on Tuesday December 18, 2018 @12:30PM (#18708483)
People confusing this terminology is one of my pet peeves. And it's not a petty one. It often impedes the conversation they're trying to have.
Re:Reasons Python Sucks by civility on Tuesday December 18, 2018 @12:31PM (#18708505)
You're right that you can't perform swaps with a Python function, but it's never pass-by-value like Java primitive types. It feels like pass-by-value because int, long, float, complex, and string are basically immutable.
Re:Reasons Python Sucks by szatkus on Tuesday December 18, 2018 @01:19PM (#18709003)
I may be missing something, but I can't find any example in my mind which indicates difference between passing in Java and Python.

And it's pass-by-value for all types, because for objects an address is the value.

Re:Reasons Python Sucks by civility on Tuesday December 18, 2018 @01:53PM (#18709370)
Java primitives (short, float, double, etc...) are copied to the calling function, but arrays and objects are passed by reference. This is easier to explain in a language like C++, which can do either:

    // Java always does these:
    void f(Object& o) { ... } // passes objects by reference
    void g(double x) { ... }  // primitives are copied

    // Java can not do these:
    void p(Object o) { ... }  // copy of the object
    void q(double& x) { ... } // reference to primitive
CPython always passes by reference (pointer), and a C extension can break the rules and modify the otherwise immutable types (int, float, complex, string, etc...).

You can squint and say everything is pass-by-value because you can always pass pointers, but then we're really losing any meaningful definitions for what semantics we're describing. I mean, it's all pass-be-electron if you dive deeply enough.

Re:Reasons Python Sucks by baddox on Tuesday December 18, 2018 @02:33PM (#18709748)
> Java primitives (short, float, double, etc...) are copied to the calling function, but arrays and objects are passed by reference.

Not really. "Pass by reference" is a specific term for a much different technique which is very rarely encountered these days. It means that if you pass a variable "foo" to a function, that function can assign "foo" to some other value and the value of "foo" will also be changed outside of that function.

The confusion, I think, is that true pass by reference is almost never encountered in modern programming languages and courses, and some people mistakenly use the term "pass by reference" when explaining the difference between e.g. primitive types and objects in Java.

Java passes everything by value. The value of a variable assigned to an object is indeed a "reference" to that object in memory, but that's a coincidental use of the term "reference."

Re:Reasons Python Sucks by civility on Tuesday December 18, 2018 @02:50PM (#18709887)
> "Pass by reference" is a specific term for a much different technique which is very rarely encountered these days.

C++ isn't all that rare.

I suspect we'll have to agree to disagree whether your definition of pass by reference is universal. Given your definition, can you name any language with calling semantics which are not pass-by-value? I mean, you're always passing the value of something (be it the actual value, the address of the memory where the data is stored, or the pointer to the string containing the name of the data in an associative array).

Re:Reasons Python Sucks by minitech on Tuesday December 18, 2018 @04:50PM (#18710913)
Popular as it is, C++ is still a pretty special case. Not many languages have references like it. (I personally don’t use any.)

  int a = 5;
  int& b = a;
  b = 6;
  // a is now 6
Allowing an lvalue argument to be changed by a function call is something more languages support, though, like C# and VB.NET. That’s what it means to “pass by reference”. Passing a reference/pointer is an accurate description of how most languages work, which might sound similar, but is really different enough to warrant not being called that. Especially because the behaviour has nothing to do with passing: variables just work that way in general in Python, Java, JavaScript, Ruby, C#, VB.NET, Lua, ….

Anyway, back to:

> I may be missing something, but I can't find any example in my mind which indicates difference between passing in Java and Python.

You’re right that C extensions can break the rules, but in the real world they don’t (because that breaks everything); immutable int objects are effectively primitives. (Modern JavaScript – in strict mode – is an example of a language that hides the implementation details of primitives better than Java.)

Re:Reasons Python Sucks by baddox on Tuesday December 18, 2018 @06:09PM (#18711475)
I don’t know any C++, so forgive me. I can’t name any language that doesn’t use pass by value. Okay, I’ve heard that FORTRAN does, but I’m not sure. But that’s the point. The distinction was made when both techniques were popular, since it was obviously an important distinction to understand.
Re:Reasons Python Sucks by mark-r on Tuesday December 18, 2018 @11:04PM (#18712913)
Early FORTRAN programs could break if you passed a constant into a function and reassigned the value within that function, because the constant would change.
Re:Reasons Python Sucks by szatkus on Tuesday December 18, 2018 @02:11PM (#18709577)
My C++ is a bit rusty, but AFAIR `void f(Object& o) { ... }` would allow you to replace the whole object in passed variable (`o = o2`). You can't do this in Java, you can only modify its fields.

It's more like `void f(Object* o) { ... }`

Re:Reasons Python Sucks by civility on Tuesday December 18, 2018 @02:29PM (#18709714)
You're right, it is more like a pointer. In C++, that would call the assignment operator which can do anything it wants, but the address of the Object would not be replaced. Java would change the value of the pointer in the callee.

These cross-language comparisons have too many nuances for me to try and make simple examples. Really, my original point is that CPython does not have "primitive types" as in Java. Everything is passed by pointer to the PyObject.

Re:Reasons Python Sucks by blattimwind on Tuesday December 18, 2018 @01:41PM (#18709257)
> , but it's never pass-by-value like Java primitive types

That's because this class of types doesn't exist in Python.

Re:Reasons Python Sucks by civility on Tuesday December 18, 2018 @01:58PM (#18709432)
I'm not sure what your point is...
Re:Reasons Python Sucks by szatkus on Tuesday December 18, 2018 @03:03PM (#18710034)
Primitive types in Java are special. Like you have boolean and Boolean. You can't invoke methods on primitive types.

I think there were plans to change it in future versions of Java.

Re:Reasons Python Sucks by jonnycomputer on Tuesday December 18, 2018 @08:52PM (#18712387)
ditto