3 ms·
> 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 havi
by inetknght 5y 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.