3 ms·
Expert perspective here (I suppose the submitter != the author, but I'm offering a critique aimed at the author): This tries to explain too much at once. It wo
by zahlman 2y ago
Expert perspective here (I suppose the submitter != the author, but I'm offering a critique aimed at the author):
This tries to explain too much at once. It would be better to work through the concept of reference semantics for variables (all the `sys.getrefcount` stuff) separately, and the idea of mutating and non-mutating methods (and, by extension, immutability of objects) separately. What's written here would be good as the first draft of a long lecture (where you have a captive audience and can build on things you just said instead of on previous articles), but IMO is not focused or organized properly for a blog piece. (It seems like the necessary pieces for this might already exist, even - given the link to the "white room analogy" etc.)
Also, pedantically Python is pass-by-value, by the original definitions of the terms. Just like how objects in Java are (https://stackoverflow.com/questions/40480 https://stackoverflow.com/questions/40480 ; https://javaranch.com/campfire/StoryPassBy.jsp https://javaranch.com/campfire/StoryPassBy.jsp), or `class` instances in C# are (https://jonskeet.uk/csharp/parameters.html https://jonskeet.uk/csharp/parameters.html). It's just that the value being passed has reference semantics, which causes surprising results. The terminology wasn't designed to account for that, and was coined in an era where value semantics were dominant. Of course, Lisp has reference semantics, because they're pretty well required to support dynamic typing (of the sort Python has). But Lisp is also strongly oriented towards immutability, and is much more descriptive than imperative, so the topic doesn't come up as much.
"Pass by assignment" doesn't mean quite the same thing as "pass by sharing" (it would be nice to attribute the latter term explicitly to Barbara Liskov, BTW), and isn't as descriptive. The connotation is instead that passing a parameter to an argument does the same thing as assigning a value to a variable. It's true that Python does work this way - but so does C++, where pass-by-reference is implemented by having an entirely separate category of reference types (which are not just the types of variables with reference semantics as above - and arguably not types at all, since the corresponding variables might not actually be represented by any kind of storage in the corresponding code). It doesn't actually describe the reference-semantics-plus-pass-by-value behaviour. Rather, in languages that don't have a special hack like C++'s, it really means the same thing as pass by value. Nowadays, I can't even think of a language in common use that isn't "pass by assignment".
Finally, while it's good to note that this system can cause confusion, the end of the summary is not really a good place for it. People who read through that much should be praised for learning something (and should have actually done so, of course), not reassured that it's okay to still not understand.
That said, the extended "recipe" example is quite nice. I find it reminiscent of something Raymond Hettinger might use in a talk.