[Tutor] beginning to code

Steve D'Aprano steve+python at pearwood.info
Sat Sep 23 22:13:44 EDT 2017


On Sun, 24 Sep 2017 08:18 am, Bill wrote:

> All one has to do, I think, is consider (1) that passing objects by
> "making copies" of them, would be prohibitively expensive 


Swift passes certain values (but not others!) by value and makes a copy. That
includes many potentially large data types including strings, dictionaries and
arrays, but using copy-on-write so the data isn't physically copied until you
actually mutate it. From the Swift documentation:


    The description above refers to the “copying” of strings, arrays,
    and dictionaries. The behavior you see in your code will always
    be as if a copy took place. However, Swift only performs an actual
    copy behind the scenes when it is absolutely necessary to do so.
    Swift manages all value copying to ensure optimal performance, and
    you should not avoid assignment to try to preempt this optimization.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html


So I maintain that one could design a language similar to Python except that
objects are assigned and passed by value, making copies only when actually
needed using copy-on-write. Swift is *almost* that language: the difference is
that Swift distinguishes between "structs" that are copied, and "objects" which
are not.


> and consider 
> that something else has to happen as an alternative, and (2) understand
> that in Python, objects don't have names, they have references (which
> have names).  The rest could be "implementation dependent" (no?)

No.

There are many different alternatives for "something else", and while some of
them may be behave the same in some circumstances, they do not behave the same
in all circumstances. Any interpreter calling itself Python would be expected
to match the behaviour of the reference implementation, CPython.

For example, if I made "Pass-By-Reference Python" where all argument passing was
done by reference, my language would differ from real Python:


function(x, y)  # allowed
function(namespace.x, module.y)  # allowed
function(x + 1, 2)  # FORBIDDEN: can't pass expressions or constants


Obviously that's not Python!

On the other hand, "Pass-By-Name Python" would allow passing expressions and
constants, but will differ in other ways.

Assignment by reference would mean that name binding was an *alias* operation:


module.y = 1
x = module.y  # x is an alias for the name "module.y"
x = 2  # binds 2 to module.y
assert module.y == 2




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list