On Sun, Jan 6, 2013 at 12:06 AM, someone <newsbo...@gmail.com> wrote: > On 01/05/2013 12:47 PM, Chris Angelico wrote: >> You can find good references on the subject in various >> places, but call-by-reference as implemented in Pascal simply doesn't >> exist in most modern languages, because its semantics are way >> confusing. The name given to this technique varies; here's a couple of >> links: >> >> http://effbot.org/zone/call-by-object.htm >> http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing > > If you don't like calling it "pass-by-reference", perhaps you prefer calling > it: “call by object reference“... From: > http://effbot.org/zone/call-by-object.htm > > In any case I think we understand each other.
That's one of the links I just posted :) It's not just a naming difference, though. With Pascal's pass-by-reference semantics, this code would act differently: def foo(x): x = 5 a = 2 foo(a) print(a) Python prints 2, because the assignment to x just rebinds the name inside foo. True pass-by-reference actually changes the caller's variable. C can achieve this by means of pointers; in Python, you can pass and mutate a list, thus: def foo(x): x[0] = 5 # Dereference the pointer, kinda x=[None] # Declare a pointer variable, ish x[0] = 2 foo(x) # Don't forget to drop the [0] when passing the pointer to another function print(x[0]) # Prints 5. See? We have pass-by-reference! But otherwise, rebinding names in the function has no effect on anything outside. Among other things, this guarantees that, in any situation, a name referencing an object can be perfectly substituted for any other name referencing the same object, or any other way of accessing the object. def foo(lst): lst[0]=len(lst) x = [10,20,30] y = x foo(x) # These two... foo(y) # ... are identical! This is a philosophy that extends through the rest of the language. A function returning a list can be directly dereferenced, as can a list literal: def foo(): return [0,1,4,9,16] print( ["Hello","world!","Testing","Testing","One","Two","Three"][foo()[2]] ) This is a flexibility and power that just doesn't exist in many older languages (C and PHP, I'm looking at you). Object semantics make more sense than any other system for a modern high level language, which is why many of them do things that way. ChrisA -- http://mail.python.org/mailman/listinfo/python-list