Student Login

Oozing between syntax and idioms

Powerful Python reader Emmanuel asks a great question about the Levels of Python Code:

"Thanks for sharing this. Can we also refer to the break statement as in the idiom level? Or is it syntax level?"

To review, the 4 levels are

1) Syntax
2) Idioms
3) Design patterns
4) Architecture

So, with code like this:

  1. # Process lines in a file.
  2. # Stop when you hit a blank line.
  3. with open(path) as handle:
  4. for line in handle:
  5. if line.strip() == "":
  6. break # No more lines
  7. do_something_with(line)

How would you classify that "break" statement? Is it syntax, or is it an idiom? Is there even a "correct" answer?

What I replied:

"That's a great fun question.

When I say there are 4 levels of Python code, that is a mental model that is more simplified and less nuanced than the full reality of complex code...

Which means we can identify aspects of Python that may not fit cleanly into one of those 4 categories.

I think "break" is a good example of this. I would say it's between the syntax and idiom levels, but closer to the syntax level. But good arguments could be made for other interpretations."

The reality is, much of what's powerful about programming spans all these levels, oozing between them.

And you might like the Powerful Python Book because of how it focuses on, er, this ooze.

For Teams Bootcamp