5 ms·
Genuine question: how do you teach beginners about python's value and reference semantics if they don't know what a pointer is? When I learned python, I knew a
by patrick451 4y ago
Genuine question: how do you teach beginners about python's value and reference semantics if they don't know what a pointer is?
When I learned python, I knew a but of C, but was certainly no expert. I remember finding it incredibly confusing that I could pass a list or instance of a class to a function, mutate it, and the caller would see that mutation, but the same did not hold for say a float. The only way I could make sense of it was to mentally classify certain types of objects as "getting secretly passed by pointer", and others as getting evidently passed by value. Without at least a familiarity with C, I think that would have been much harder to understand.
- inetknght 4y ago> how do you teach beginners about python's value and reference semantics The great thing about Python is that it's super easy to learn by example without having to troubleshoot cryptic compiler errors first. So show them a few objects. Modify those objects. Show them how to deep-copy objects and how to modify objects by reference without a deep-copy. Once the examples have been made then start asking about how they might write their own, in Python, types that could be deep-copied or reference-modified. Make sure to include your own questions about classes, class attributes, function parameters, initialization, and forced cleanup using either `del` or `with` -- it's handy to use a file object for examples of object destruction since that's buffered and so writes to it won't show up until it's flushed or closed (which occurs automatically when the file object is destroyed). Then you might expand to questions like: if you have a property somewhere that points to "some reference" but the reference could change to one of several different objects or maybe even to none of the objects, how would you do that? Is the property maybe pointing to one of the other objects? There are many ways to do this and most of them are "valid" for mentoring. What are some other ways to do it and what are some of pitfalls or benefits to the other ways? From there you can segue your way into describing what a pointer is "under the hood" if you think the student is ready for more info about pointers.
- DerekL 4y agoYou teach them how it works in Python. You don't take a detour to an entire other language.
- edflsafoiewq 4y agoThat's not how Python works so trying to map it onto value/reference categories actually leads you astray. There is no pass by value, all objects are shared between caller and callee. But since a float is immutable, there is no internal state you can change that can be observed by the caller. If you do x += y, i.e x = x.__iadd__(y), the distinction is actually between immutable objects where iadd returns a new object, and mutable objects, where iadd modifies x and then returns x.
- inetknght 4y ago> That's not how Python works so trying to map it onto value/reference categories actually leads you astray You cannot teach physics to five year olds by, for example, starting with Euler's equations. Python doesn't work the same way as C, sure. But it works fine enough to get a feeling for how something "might" work. Then when they move on to another language, just as graduating to a harder class, you might have to un-learn some of the things to learn how it's really done.