4

I read this article about Python's variable bindings. It was criticized for teaching errouneous hypotheses. It stated that Python is neither "call by reference" nor "by value".

Someone said in the comments that Python actually follows a clear "call by value" model, where "value" is an object pointer. I do not get the difference between this and calls by reference.

This is not about the difference between "by value" and "by reference", it is about the difference between "by value (object pointer as value)" and "by reference".

Let's say that you hand over a variable by reference. Isn't this reference a value being passed, pointing to a target? Why is it different?

I think that I know what works in Python and what not, but this is somehow an unclear differentiation to me.

Thank you!

Xiphias
  • 4,468
  • 4
  • 28
  • 51
  • Related, possible dupe: [Python: What is the difference between Call-by-Value and Call-by-Object?](http://stackoverflow.com/q/10844088) – Martijn Pieters Dec 17 '13 at 15:59
  • Python passes the "pointer" by value. Like in C, if you pass a pointer to a function, changes made to the actual pointer are done on the local copy, while changes to the object pointed to by the pointer are persistent. – Some programmer dude Dec 17 '13 at 16:00
  • Python doesn't use 'pass by reference' either; it is 'pass by object' really. A reference is to a variable location, not the value itself, for example. – Martijn Pieters Dec 17 '13 at 16:01
  • @JoachimPileborg: Thank you. I've never encountered a function making changes to a pointer itself. What would be a use case of modifying a pointer instead of the object behind it? So where would the concept make a difference? Does "by reference" also use pointers but not local copies? – Xiphias Dec 17 '13 at 16:03
  • @MartijnPieters: This is what was criticized in the article, they said that no "passing by object" exists, it would be passing "call by value (=object pointer copy)" – Xiphias Dec 17 '13 at 16:04
  • There is plenty of scope to quibble over exact terminology here, sure! – Martijn Pieters Dec 17 '13 at 16:05
  • But it is the 'call-by-value' term that is at stake here; in C, values are the contents of a memory address, so passing in an integer to another function passes the actual value of the integer. In Python, you are always passing in a complete object (where `int` is an object with an integer value). – Martijn Pieters Dec 17 '13 at 16:08
  • Other people don't see a distinction there, the object *is* the value. For people with a C background (and perhaps knowledge of Python internals), that doesn't hold up. – Martijn Pieters Dec 17 '13 at 16:09
  • @MartijnPieters: A pointer also has a memory address, hasn't it? Is it correct? Would it also be a value then? – Xiphias Dec 17 '13 at 16:15
  • 3
    Yes, but you have to explicitly dereference the pointer in C. You can perform arithmetic on the pointer too, in C; add 1 to a pointer and you are now pointing at the next memory location. Dereference it and you will get a different value. Python doesn't let you do that, so what is passed around are not exactly pointers either. – Martijn Pieters Dec 17 '13 at 16:19

0 Answers0