4 ms·
One thing I was waiting for the article to mention is that basic has a REPL. I started on Atari basic in the 80s and having a REPL, even though I didn't know t
by Nihilartikel 2y ago
One thing I was waiting for the article to mention is that basic has a REPL.
I started on Atari basic in the 80s and having a REPL, even though I didn't know to call it that, was revelatory. Writing a line of code or a little for loop and drawing lines on the screen was a very tight learning experience.
Python approaches that with its REPL and notebooks but doesn't quite match the pedagogical ergonomics I enjoyed 35 years ago with that basic cartridge.
For beginners, python also has some confusing foot guns too, like understanding which objects pass by value and which by reference. It's all part of learning, but when a beginner is still trying to get a handle on variables and functions, it can confound the fundamentals.
- pjmlp 2y agoEverything is by value. Passing by reference means this is possible: def change_param(ab): ab = 768 x = 12 print(x) change_param(x) print(x) Expected output in reference parameters, 12 followed by 768. Otherwise it is always a value, even if the value happens to be a reference to an heap allocated object. Python has no mechanism to change the contents of the x variable used as parameter on call site.
- graemep 2y agoYou can do that if the parameter is a list: def change(l): l[1] = 'x' >>> foo = [1,2,3] >>> foo [1, 2, 3] >>> changel(foo) >>> foo [1, 'x', 3]
- pansa2 2y agoYou can't change which list `foo` refers to, though. Only the contents of that list. `foo` is a reference to a list, and that reference is passed by value.
- pjmlp 2y agoNow make foo point to another list, not change contents of the list foo points to. Passing by reference requires changing the variable itself, used on the call site.
- zahlman 2y agoThis does a fundamentally different thing with similar syntax. Assignment in Python is fundamentally a rebinding operation; slice assignment rebinds a reference internal to the object, whereas ordinary assignment rebinds the name. The classic test: in Python you cannot write a generic "swap function" - you could only possibly write a function to swap the internal states of the arguments, presuming them to be compatible.
- pansa2 2y ago> even if the value happens to be a reference to an heap allocated object IIRC in CPython, the value is always a reference to an object. Other implementations might pass immutable objects directly by value, but the semantics is the same as passing a reference to the object by value.
- zahlman 2y agoYou are correct. (I'd be surprised if other implementations passed immutable objects by value, partly because representing an "object" coherently requires several words of memory and partly because of the importance of certain object identity semantics.) Python uses pass-by-value (if you prefer, pass-by-assignment) of variables which have reference semantics. Just like non-primitive types in Java, or class types in C#.
- pansa2 2y ago> I'd be surprised if other implementations passed immutable objects by value I'm thinking of MicroPython here - it supports several pointer-tagging schemes, in which "references" might be actual pointers or might instead be immutable values stored directly. For example, using the NaN-boxing scheme, MicroPython references might actually be inline `float`s, small `int`s, or constants like `None`, `True` or `False`.
- n144q 2y ago> like understanding which objects pass by value and which by reference Isn't that true for almost every "mainstream" language?