Inconsequential musing of the day:
If you look at say, a Python file — or Bash or Ruby or almost any other scripting language — you can’t find any information1 about the names defined in a program without actually running the script. This is because in Python, the only way to associate a name with a value is to execute a line of code that causes it to get some value (maybe an assignment statement, maybe a function definition, maybe an
eval that contains one of these things). The association of top-level names to values is inherently a procedural process, defined only by the language’s execution model.
Contrast this with the situation in C. C has actual declarations, which associate certain information to a name completely independently of the execution model, and in fact the execution model usually doesn’t2 cause values to become associated with top-level names at all. C’s top-level namespace is specified declaratively: it states directly which names are associated with which values.
Does this matter? In practice, probably not much. In theory, a declarative program layout should be easier to manipulate through automated tooling (for example, to list out the documentation for all of the top-level names defined in a module/file without running any of its code). But most reasonable Python programs are pretty declarative at the top level anyway: you rarely see things like
# webserver.py turing_machines = [...] if does_it_halt(turing_machines): def post(blah, bleh): blee if does_it_halt(turing_machines): def get(bloo): bluh globals()[running_time(turing_machines)].__doc__ = ''.join(accepted_language(turing_machines))
On the other hand, languages like C do make it a lot easier to catch typos, which I consider to be an appreciable benefit. Not that typos are a particularly difficult to track down class of bug.
But mostly I just found it interesting to think about how C’s model is, in a way, more declarative than lots of more modern languages.
- In general. Of course most specific scripts aren’t quite so horrible. ↩
I think there are a couple exceptions to this having to do with
statictop level variables and perhaps also function pointers (can those be defined at the top level?). But the top level of a basic C program consists entirely of function declarations and no assignment. ↩