nonlocal fails ?

Richard Damon Richard at Damon-Family.org
Fri Nov 15 11:47:40 EST 2019


On 11/15/19 11:04 AM, Random832 wrote:
> On Fri, Nov 15, 2019, at 10:48, Richard Damon wrote:
>> On 11/15/19 6:56 AM, R.Wieser wrote:
>>> There are quite a number of languages where /every/ type of argument 
>>> (including values) can be transfered "by reference".  Though some default to 
>>> "by value", where others default to "by reference".
>> It seems you are stuck in a programming model different than what Python
>> provides, In a very real sense, the terms call "By Reference" and "By
>> Value" don't apply to Python. Those concepts presume that a variable
>> name represents a bucket of bytes that holds some value, and call by
>> value copies those bytes into a new variable for the subroutine, and
>> call by Reference creates a pointer value back to original value, and
>> all uses of that parameter work indirect that pointer.
>>
>> That is NOT how Python works. In Python, in effect, every name in your
>> code is just a reference which points to some object (This is called
>> binding). Multiple names can point to the same object (or no names, at
>> which point the object is subject to being garbage collected). Names
>> themselves aren't objects, so you can't really make one name refer to
>> another, only to the same object that the other one does. (In actuality,
>> these collections of names are implemented basically in a Dictionary, so
>> using this sort of implementation details you can sort of get that
>> result, but I don't think that is defined to work in the language).
> Being abstractly typed objects rather than a bucket of bytes, and having the values themselves be a kind of reference or pointer (though to immutable data in some important cases), does not in fact change the meaning of the "call by reference" concept or its applicability.
>
> It would be entirely reasonable, I think, in the python model, to provide a way for making a variable a cell variable, passing the cell object around explicitly, and having the called function automatically set/get the value when the argument is accessed. I don't think it would solve the OP's problem, since his talk about automatically "inheriting" the caller's variable of the same name sounds a lot more like dynamic scope than call by reference.

It isn't hard to explicitly do this in Python. The caller just puts it
value into a 1 element list, and then the called function mutates that
list (setting parm[0] instead of parm). This appears to align with
standard Python idioms.

The issue with calling it a Reference, is that part of the meaning of a
Reference is that it refers to a Object, and in Python, Names are
conceptually something very much different than an Object. Yes, in the
implementation details, a name is basically a dictionary entry, so it
could be done, the question is should it.

I have learned many computer languages over the years, and can say that
while figuring out how to map constructs from one language to another
has some value to start, you really do want to learn how the language
really working in itself. There is an old saying that you can write
FORTRAN in any language, and in a real sense it is true, and applies to
many languages. In a general sense you can generally take a programming
style from language X, and use it in language Y, and often be able to
get a 'working' and 'valid' program, but your really should be using
language X structures and use them in language X, write language Y using
the normal style for language Y.

In general, if you want to write a 'x' program, use language x, not
language y. The major exception is if language x isn't available on the
platform, but Python is usually the fallback language to go to, that
will more likely be C or C++ as those are what are most often the first
languages that are brought to a platform. (So maybe learning how to
write Python is C is useful, but writing C in Python has less need),

-- 
Richard Damon



More information about the Python-list mailing list