Programming Principles (w. Python Examples)
Programming from the ground up for someone who has never written any — what a name is, what running your code actually does, and how to ask a live program what it is doing. Every idea comes with Python you can run in the page.
Sections
- §01
Names, objects, and memory
Names are labels, not boxes.
Assignment binds a name to an object; it never copies one. Watch two names end up pointing at the same list, mutate it through one of them, and see the other change too. Identity versus equality, and why rebinding a name is a different act from mutating the thing it points at.
- §02
The file, the process, and the session
A program is a process, not a text.
What actually happens when you type `python hello.py`, and how that differs from starting a bare `python` session, from `python -i hello.py`, and from importing the file. Why the session remembers what the script forgets — and why `if __name__ == "__main__":` stops being magic once you have felt the difference.
- §03
Functions and calls
A function is a value you can call.
`def` does not run anything; it builds an object and binds a name to it, exactly like assignment. The body waits for a call. Parameters are fresh names in a namespace that appears on the call and vanishes on return — which is the whole of scope, and the difference between `return` and `print`.
- §04
Errors and tracebacks
An error is a message, not a punishment.
Three tiers of wrong, in increasing order of nastiness: the code will not parse, the code runs and stops, the code runs and lies. Reading a traceback from the bottom up, and why the loudest failure is the kindest one.
- §05
Interrogating a running program
Don't guess — ask.
You cannot tell what a program does by staring at it. `print`, `type`, `dir`, `help`, and a live session are how you put questions to a running program and get answers back. Handed a mystery object, work out what it is.