News for hackers. Stuff that matters.
Re:Python, as Reviewed by a C++ Programmer by srean on Sunday April 23, 2017 @12:50PM (#14178933)
Claims that Python/C#/Java etc., are call by value where the 'value' is the value of the pointer does not help. It only muddies things. There really ought to be different name for such call semantics and there is. Its called call by object. In C++ lingo call by const pointer comes closest: one cannot change what the pointer points to. However the pointed object may not be a const
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:02PM (#14178999)
No, no, no there is no "call by object." It is precisely this mistaken idea which muddies the waters, as you put it. Virtually all modern processors are stack-based architectures. All arguments to functions are passed on the stack or in registers. The only thing that can be placed onto the stack or into a register are numbers. Those numbers can represent either an actual value or an address in memory. The former is call by value, the latter is call by reference. All high level languages that allow calling functions or methods with arguments have to ultimately use this facility to pass those arguments. Python is most definitely call by reference.
Re:Python, as Reviewed by a C++ Programmer by oliver_quartic on Sunday April 23, 2017 @01:12PM (#14179073)
Couple of things:

1. "Call/pass by XXX" terminology usually refers to language semantics, not underlying implementation details. In that respect, the call-stack mechanism is largely orthogonal here.

2. According to this oft-cited article (in the context of Java), passing an address under the hood is not sufficient to make a language call-by-reference. http://javadude.com/articles/passbyvalue.htm - AFAIK Python would "fail" the "Litmus test".

(EDIT - clarify what's not call-by-ref)

Re:Python, as Reviewed by a C++ Programmer by Al-Khwarizmi on Sunday April 23, 2017 @01:24PM (#14179149)
In Java, this code will print the string representation of Lassie:

  Dog toby = new Dog("Toby");
  
  public static void replaceDogWithToby ( Dog d )
  {
    d = toby;
  }
  
  public static void main ( String[] args )
  {
    Dog d = new Dog("Lassie");
    replaceDogWithToby(d);
    System.out.println(d);
  }
In Pascal, this will print Toby:

  var
    toby:tDog;
  
  procedure replaceDogWithToby ( var d:tDog );
  begin
    d := toby;
  end;

  var
    lassie:tDog;
  begin
    {code to initialize toby and lassie here}
    d := lassie;
    replaceDogWithToby(d);
    writeln(getDogName(d));
  end.
The first is call by object (or call by sharing, in my book). The second is call by reference. The behavior is totally different.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:44PM (#14179272)
I haven't done java in a long time so I won't try to debate the specific language level semantics with you (the same is true of pascal though it has been even longer), however assuming that your examples run and behave as you describe then what is going on in the java example is that 'Dog d' is passed as a reference to a reference, and then the value pointed to by the argument, i.e. the address of "toby," is replaced with the address of "lassie."
Re:Python, as Reviewed by a C++ Programmer by E6300 on Sunday April 23, 2017 @02:01PM (#14179393)
You got it backwards. The Java snippet will print "Lassie". In Java, object names are references to objects, and passing them to functions copies the references, so of course modifying the formal parameter (d from replaceDogWithToby()) doesn't modify the actual parameter (d from main()).
Re:Python, as Reviewed by a C++ Programmer by abhirag on Sunday April 23, 2017 @01:27PM (#14179165)
"Variables in Python are object references. When you call a Python function the arguments are copies of the references to the original object." -- I personally wouldn't worry about what to call it(though call by reference would be my pick too), in the end I just visualize it as being similar to how assignment works in Python.
Re:Python, as Reviewed by a C++ Programmer by mark-r on Sunday April 23, 2017 @01:34PM (#14179214)
The confusion comes from the difference between references in Python and C++. In Python you can reassign a reference to a different object any time you want. In C++ you can't, and any attempt to do so modifies the referenced object instead. That fundamental difference makes it hard to understand the mechanics of Python using the terminology of C++.
Re:Python, as Reviewed by a C++ Programmer by dboreham on Sunday April 23, 2017 @01:13PM (#14179076)
Yes. But. Language designers go out of their way typically to hide this underlying truth, by for example implementing "call by value" when the type of the value is "Customer" or whatever.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:31PM (#14179198)
It's specifically because high level language semantics often mask the underlying implementation that knowing that implementation is necessary for truly understanding what's happening. The situation you describe is call by reference, where the value passed on the stack or in a register is a reference to a customer object. You could pass a customer object by value (in languages that allow it, such as C++) by copying the structure verbatim onto the stack and addressing it there.
Re:Python, as Reviewed by a C++ Programmer by monochromatic on Sunday April 23, 2017 @01:11PM (#14179063)
It may be implemented as call-by-reference, but the semantics are not the same as what one thinks of when he hears that phrase.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:27PM (#14179168)
In what way are they different?
Re:Python, as Reviewed by a C++ Programmer by Pica_soO on Sunday April 23, 2017 @04:10PM (#14180197)
Im mostly experienced with the Lua Reference handling, so beware.

Usually there is one global hash-table, and a local hash table. Everything (every function, every object, every string is stored once, depending on the calling situation. So passing a reference to a object is passing the reference to that one global object, until you change it, or make a local copy of it. That sounds really strange and in a way is it. It also means, that your list of objects, will not contain duplicates. It means no useless work is done to copy data.

The model involved borrows basically from the concept of lazy loading and flat copys. Do work only when needed. Super difficult to explain to new users without CS background.

Re:Python, as Reviewed by a C++ Programmer by mark-r on Sunday April 23, 2017 @01:37PM (#14179229)
In a classic call-by-reference, changes you make to the passed parameter are seen by the caller when the function returns. That doesn't happen consistently with Python.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:48PM (#14179289)
I think you're just using "classic" to mean the behavior of languages like C++. Under the hood there are only two ways this mechanism can function, and I don't think it helps to mix high level language semantics in when trying to explain it, as those semantics are always expressed in one of those two ways.
Re:Python, as Reviewed by a C++ Programmer by mark-r on Sunday April 23, 2017 @11:45PM (#14181830)
I'm using "classic" to refer to the way things were done when the terms were first invented. Old languages like C or Fortran.

Python uses references implicitly without ever defining the term, so it's essential to talk about the high level semantics in this case. A Python reference (implied) differs greatly from a C++ reference in that it can be rebound with the assignment operator. If you're thinking about references in C++ terms then Python will confuse you. It's important to call out the differences.

Re:Python, as Reviewed by a C++ Programmer by monochromatic on Sunday April 23, 2017 @07:17PM (#14180972)
With anything immutable.
Re:Python, as Reviewed by a C++ Programmer by srean on Sunday April 23, 2017 @01:38PM (#14179235)
processors and architectures have nothing to do with this. This is a matter of semantics. The prolem stems from different communities appropriating the same english word to mean subtly different things, hence my clarification "in C++ lingo ..."
Re:Python, as Reviewed by a C++ Programmer by Al-Khwarizmi on Sunday April 23, 2017 @01:15PM (#14179098)
Scott's book "Programming Language Pragmatics" calls it "call by sharing" IIRC. BTW, that book describes like 6 or 7 modes of parameter passing. Languages like Ada have some quite original modes.
Re:Python, as Reviewed by a C++ Programmer by user5994461 on Sunday April 23, 2017 @01:27PM (#14179163)
Python is "pass reference by value", in C++ semantics.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:28PM (#14179177)
Of course, because that is the only way to implement call by reference, i.e. to pass the reference as a value.
Re:Python, as Reviewed by a C++ Programmer by user5994461 on Sunday April 23, 2017 @01:42PM (#14179260)
It's possible to pass a reference by reference.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:50PM (#14179312)
In which case the argument is still a value, containing a pointer to a value, which is a pointer to the object.
Re:Python, as Reviewed by a C++ Programmer by user5994461 on Sunday April 23, 2017 @03:18PM (#14179892)
In C semantics, that's about it.