4 ms·
> Python variables aren't names for storage locations to begin with; they're namespace bindings. Can you explain the difference? What is a "namespace binding"?
by tom_mellior 8y ago
> Python variables aren't names for storage locations to begin with; they're namespace bindings.
Can you explain the difference? What is a "namespace binding"? When accessing a variable's value, the interpreter's code sure looks like it treats the variable name as the name of a storage location.
> Passing a variable to a function means passing a particular namespace binding
"Passing a variable to a function" is not a thing. Passing a value to a function means passing a pointer to that value. This is the same for "foo(some_var)" and "foo(1)". Since "1" is presumably not a "namespace binding", you seem to suggest that there are different parameter passing mechanisms for these cases. There aren't.
- kzrdude 8y agoEvery value in Python is a reference to an object. All function parameters are such values that are passed as they are. You get the same references to objects that the caller passed. We could call this "passing references to objects by value."
- zephyrfalcon 8y agoOver the years, I have found this article to be useful: http://effbot.org/zone/call-by-object.htm http://effbot.org/zone/call-by-object.htm
- Izkata 8y agoThat's a long form of what I most often hear: "pass by value of reference" But in any case, that's the point that made me go wtf while reading through as well. Whatever you want to call it, it's the same as what C (when not passing pointers), Java, PHP, Javascript, and many many other languages use. Which is why it's so frustrating we don't have a good, easily-understood/well-known name for it.
- d0mine 8y agoHere are pictures that explain the difference between variables as named boxes and names in Python https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables https://david.goodger.org/projects/pycon/2007/idiomatic/hand...
- tom_mellior 8y agoThat's a nice explanation for BASIC programmers, but for anyone who has ever programmed in some other programming language than BASIC, it should not come as a surprise that named boxes can store pointer values. As they do in Python. They literally store C pointers of type (PyObject *).
- d0mine 8y agoPython is not C. There are no pointers in Python. I have no idea why would you mention BASIC here.
- pdonis 8y ago> What is a "namespace binding"? An entry in a Python dictionary that is the namespace for the current scope. For example, to repeat a response I gave to someone else upthread, compare a variable assignment in C and Python. In C: int i = 17; Means "set aside one int's worth of storage and fill it with the bit pattern for the number 17". In Python: i = 17 means "create an int object with the value 17 and bind it in the current namespace dictionary to the name i".
- tom_mellior 8y agoIn other words, the latter means "set aside one pointer's worth of storage and fill it with the bit pattern for the pointer pointing to the value 17 (which you may have to create first)". Variables have values. In C those values may be ints, floats, pointers, whatnot. In Python (at the implementation level) those values are pointers to objects.
- pdonis 8y ago> In other words, the latter means "set aside one pointer's worth of storage Set aside one pointer's worth of storage on the heap, yes; not on the stack or in the program's data segment, which is what the C statement I gave does. > and fill it with the bit pattern for the pointer pointing to the value 17 (which you may have to create first)" Which has no analogue whatever in the C version. Also, you completely left out the part about creating an entry in the namespace dictionary, which also has no analogue whatever in the C version, and which involves additional memory allocations.
- tom_mellior 8y ago> not on the stack Except for locals. (Well, OK, the Python stack is reified in the heap, but local variables are translated into small integer indices into the stack frame.) > an entry in the namespace dictionary Except for locals, which do not involve memory allocations or dictionary lookups. Anyway, we're getting farther and farther away from the topic, which was that Python argument passing is no different from any other language in which everything is an object.