5 ms·
Isn't this by definition call by value? The reference / binding is passed by value. Changing the reference to point to something else will not make the other re
by manojlds 14y ago
Isn't this by definition call by value? The reference / binding is passed by value. Changing the reference to point to something else will not make the other reference to point to the same too.
- jknupp 14y agoCall by value would mean that a copy of the value of the argument is passed to the function without involving the original value at all. To say the binding is passed by value is not useful, as all languages would then be considered "call by value", since _something_ needs to be passed into the function, and we can just call it a value.
- viraptor 14y agoThis argument is as old as call-by-... definitions, but... What python holds in the variables are references to objects. Those are the values you're passing, so it's call by value using this reasoning. Some languages do actually hold the value itself (struct for example) in the variable, so there you get a copy of the value (the struct contents). In python you get the copy of a reference (which again, is the value held in a variable).
- lvh 14y agoThat is correct: the object reference is copied into the caller's symbol table when calling. Hence, CBV.
- adrusi 14y agonot true. the program can be compiled to directly reference the variables themselves
- mistercow 14y agoPedantically, sure, but I think the point is to explain it so that people actually understand. If you say "Python is call-by-value" to C programmer then they will probably have some serious misconceptions about what happens when you pass an object to a function.
- irahul 14y ago> they will probably have some serious misconceptions about what happens when you pass an object to a function. p = Person() The thing on the left is object reference, the thing on the right is object. When you do some_func(p) or some_func(Person()), you don't pass an object but an object reference. There. No serious misconceptions. C# and Java are doing just fine having the same model and explaining it the same way. And C programmers just need to think of `p = Person()` as `Person *p = new_person()`
- mistercow 14y ago>There. No serious misconceptions. Well, yes, there might be. Because "object reference" sounds a lot like you're saying it's call-by-reference, and that's also confusing. it makes it sound like the output of this would be "overwritten": def some_func(x): x = "overwritten" p = Person() some_func(p) print p In addition, my point was that using the term "call-by-value" is confusing, and since you didn't use that term in your explanation, it's all moot.
- irahul 14y agoMy point is writing a blog post saying python is neither call-by-value nor call-by-reference is made up shit and does nothing for someone who doesn't already understand it. Someone who doesn't already understand it will have to work through to get the gist of call-by-value and object references. The blog post didn't simplify anything for someone who doesn't already understand it. So why not stick with terminology which is used by Java, C#, C among others?
- mistercow 14y ago>My point is writing a blog post saying python is neither call-by-value nor call-by-reference is made up shit I guess so, in the sense that all of those terms are "made up shit". But they are useful for explaining the concepts. >The blog post didn't simplify anything for someone who doesn't already understand it. So why not stick with terminology which is used by Java, C#, C among others? Apparently, as others have pointed out here, the term "call-by-sharing" has largely come into popular use from people explaining how Python works in user groups. That's a strong indication that it actually is a pretty effective way to help newbies understand it.