3 ms·
i have taught quite a few people of various backgrounds and levels. And still provide a short crash-course, once in a while. IMO in this article the elephant i
by svilen_dobrev 2y ago
i have taught quite a few people of various backgrounds and levels. And still provide a short crash-course, once in a while.
IMO in this article the elephant is missing, esp. for C/C++/java people: there are no variables as storage-spaces in Python. There are names/labels in namespaces, attached/referencing some value (stored in VM), and that is taken/examined at runtime-usage of those labels/names. The values themselves are living on other side of the fence, inside the virtual machine.
The most important piece of the language Python in docs is the execution model, and in it, the "naming and binding" part:
https://docs.python.org/3.11/reference/executionmodel.html#naming-and-binding https://docs.python.org/3.11/reference/executionmodel.html#n...
i.e. what constitutes a namespace, how are they stacked at declare-time or run-time (visibility/shadowing), lifetimes, scopes and all that.
The most usual gotcha is that inside of class xyz: ... is only a temporary namespace - and becomes class' attributes when closed (well, subject to metaclass behaviour).
i usually present the "storage" as "imagine 2 notebooks of ~transparent-sheets" : one is the names' visibility (so called "variables") - namespace-hierarchy, where each sheet is one namespace level, and you see only the inner-most page that has something in certain row - shadowing all those towards the root at module level; the other is the attribute-on-something visibility, where the instance (if any, of whatever type) is (only one and) always on top, and then inherited classes (not instances) underneath one under another. Also, all instance-or-static methods in Python are virtual, i.e. using the topmost available - while class-methods are not, they use the specified-or-below but not above.
Frankly, took me ~10 years, occasionally rereading that chapter each time when hitting new category of errors, to grasp the whole of it. Each comma there matters. Take your time.
Edit: this "names-only" thing is similar in javascript, and other interpreted languages. Python also exposes the whole VM-runtime - from programmer's side of the fence - i.e. locals() is the current namespace, inspect.stack() gives the current namespace hierarchy, etc. Self-reflection can be taken quite far
- jebarker 2y ago> The most important piece of the language Python in docs is... Asking as an intermediate python programmer (in my assessment!): what is the importance of knowing this? Is it essential to know to _use_ python correctly or to _understand_ the language implementation?
- svilen_dobrev 2y agoit's only about usage. The Rules of the language. implementation does not matter, Cpython behaves same as IronPython or Jython or what-have-you - at least for this basic stuff. if you use python in very procedural way, and never reuse things in time, then probably does not matter. But, thing is, python is not very procedural, and, most of the stuff happens and depends in time as well as in space/storage/memory. And those rules draw the boundaries which is where and when, and why. One weird way of looking at these maybe that "variables" - i.e. names - are somewhat like attributes of some superficial "instance" called namespace, with leaner syntax; but with different behavior than plain attributes on things (btw those are quite deep). And there no other ways to access and juggle values/things - so one better knows them. Read that chapter once. You may or may not grasp everything (and don't need to). Then forget about it. Next time you step on weird error of some xyz being 4 when it was assigned 67, re-read it. Repeat..