5 ms·
Interesting perspective on some of python's features, but I hate to see this "python is neither call by value nor call by reference" silliness perpetuated with
by markbnj 10y ago
Interesting perspective on some of python's features, but I hate to see this "python is neither call by value nor call by reference" silliness perpetuated with a link to the original silly blog post. I'm surprised a C++ programmer was caught by that obviously incorrect assertion.
- srean 10y agoClaims 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
- markbnj 10y agoNo, 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.
- abhirag 10y ago"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.
- monochromatic 10y agoIt may be implemented as call-by-reference, but the semantics are not the same as what one thinks of when he hears that phrase.
- markbnj 10y agoIn what way are they different?
- mark-r 10y agoIn 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.
- markbnj 10y agoI 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.
- mark-r 10y agoI'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.
- Pica_soO 10y agoIm 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.
- oliver_quartic 10y agoCouple 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 http://javadude.com/articles/passbyvalue.htm - AFAIK Python would "fail" the "Litmus test". (EDIT - clarify what's not call-by-ref)
- dboreham 10y agoYes. 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.
- markbnj 10y agoIt'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.
- Al-Khwarizmi 10y agoIn 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.
- markbnj 10y agoI 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."
- mlvljr 10y agoYou got it wrong -- it starts as Lassie and stays as such after the function call (as "replacement" only affects the local (stack) copy of the pointer / refernce passed) :) No fancy pointer-to-pointer business thus. No idea what's going on Pascal, though, have to check out myself. upd: upon checking http://wiki.freepascal.org/Variable_parameter http://wiki.freepascal.org/Variable_parameter , I suspect that here a pointer is passed underneath again, but the assignment now copies the bytes over into the pointed-to object.
- E6300 10y agoYou 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()).
- mark-r 10y agoThe 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++.
- srean 10y agoprocessors 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 ..."
- Al-Khwarizmi 10y agoScott'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.
- user5994461 10y agoPython is "pass reference by value", in C++ semantics.
- markbnj 10y agoOf course, because that is the only way to implement call by reference, i.e. to pass the reference as a value.
- user5994461 10y agoIt's possible to pass a reference by reference.
- markbnj 10y agoIn which case the argument is still a value, containing a pointer to a value, which is a pointer to the object.
- user5994461 10y agoIn C semantics, that's about it.