If you look at Python tutorials and sample code, proposals for new language features, blogs like this one, talks at PyCon, etc., you'll see spam, eggs, gouda, etc. all over the place. Why?

Metasyntactic variables

If you're writing some toy code that doesn't do anything (e.g., it just demonstrates some syntax), there are obviously no meaningful names to give the variables and types in that code. What you need are words that are obviously meaningless, and obviously placeholders for the meaningful names that you'd use in real code.

Of course there are no such words (except maybe "um" and "like" and the like), so the programming community has to invent a few and use them by convention. These are called metasyntactic variables; Wikipedia explains why they're called that, and some of the history. In other languages, they're usually called foo, bar, baz, and qux.

Python has its own unique set of metasyntactic variables, which are actual words, but words unlikely to appear in normal code. This has the advantage that the pattern can be extended in new ways and everyone will intuitively know what you mean. Except, of course, that you have to know the pattern.

Spam, eggs, cheese, beans, toast, and ham

Python is named after Monty Python, because Python's inventor, Guido van Rossum, is a big fan, like many computer geeks. "Spam" is one of Monty Python's most famous skits. Most of the words in the skit are the repetitive names of the heavily-spam-focused breakfast dishes on the menu, plus a group of Vikings singing a song about Spam. So, Python uses the ingredients of those dishes for its metasyntactic variables.

Here's the menu:

  • egg and bacon
  • egg, sausage, and bacon
  • egg and spam
  • egg, bacon, and spam
  • egg, bacon, sausage, and spam
  • spam, bacon, sausage, and spam
  • spam, egg, spam, spam, bacon, and spam
  • spam, spam, spam, egg, and spam
  • spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam, and spam
  • lobster thermidor aux crevettes with a Mornay sauce, garnished with truffle paté, brandy, and a fried egg on top, and spam
So, you can see where the standard metasyntactic variables in Python come from.

Well, almost. Cheese, ham, and toast aren't even mentioned in the skit (although ham does appear in one of the silly names in the credits of the episode), and beans only appears once, while bacon and sausage are all over the place. So, why?

Back in the early 90s, we didn't have YouTube and t'Netflix. In them days, near 30 year ago, if we wanted to watch our favorite old shows, we were glad to find them on videotapes. Without subtitles. Or readable picture quality. Or, often, videotapes. And that was if you had a VCR. We never had a VCR, you used to have to spool the tape by hand and try to read it with a compass needle. If you were lucky enough to have hands, that is, we couldn't afford 'em. Still, we were happy in them days. We couldn't go to t'Wikipedia on t'World Wide Web to look up information we'd forgotten. We had t'Yorkshire Wide Web, which had nought but an ASCII art picture of a terrier, only it was in EBCDIC, mind, so you had to translate it by hand. We had to remember things for ourself. Except we were too busy remembering other people's things. Only job you could get back then. Paid thruppence a week, working 24 hours a day. Started when we were three years old and got our first lunch break at six. We'd get a crust o' stale bread and back to work for t'next three years. 'Course lunch break wasn't paid, we had to pay the owner to take the time off, then pay him to come back to work, but it was a living, and we were happy to have it. We could take the money home to our Dad and maybe he'd only kill us and dissolve our bones in acid once or twice before supper. You try and tell the young people of today that, and they won't believe you.

When you need a superclass of Spam, Eggs, and Cheese, or a class that has spam, eggs, and cheese members, you've got Breakfast, and sometimes Menu. Since none of these types have any obvious verbs associated with them, when you need to talk about methods, you'll occasionally see waitress.serve(spam) or song = [viking.sing() for viking in vikings].

Gouda and edam

Guido van Rossum comes from Holland. Not the Netherlands, the part of the Netherlands called Holland. Although he's also from the Netherlands. And he's Dutch, too. Maybe this is why the most famous line in the Zen of Python is "There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch."

Gouda and Edam are two famously Dutch cheeses. So, when you need to create meaningless instances of a meaningless type in your example code, the type is usually Cheese, and the instances will be gouda and edam, because those are two famously Dutch cheeses. (If you need more, use some other Dutch cheeses. Just don't use griene tsiis or nayl tsiis, because those aren't really food, they're just something that Frisians invented so they can claim their language sounds like English.)

Tulips are also an important part of Dutch history, especially in the North Holland area that Guido is from. Most famously (if not accurately), the Dutch cornered the market on tulips and then created a speculative bubble that ruined their economy, which is what allowed the English to take over as the leaders of the financial world. So, obviously, that's what the prerelease version of the new asyncio library for Python 3.4 was called. But don't eat tulips, as they're mildly toxic and taste horrible.

Counting to four three

Sometimes, examples will be numbered 1, 2, and 4. Especially when someone's making a point about 0-based vs. 1-based indexing. This is a reference to the running joke in Monty Python and the Holy Grail, there's a running joke about Arthur having problems counting to three. In particular, the Holy Hand Grenade of Antioch scene has a biblical quote from Armaments 2:9-21:
And the LORD spake, saying, 'First shalt thou take out the Holy Pin. Then shalt thou count to three, no more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, neither count thou two, excepting that thou then proceed to three. Five is right out. Once the number three, being the third number, be reached, then lobbest thou thy Holy Hand Grenade of Antioch towards thy foe, who being naughty in My sight, shall snuff it.'
There's also a joke from the Spanish Inquisition skit, where one of the cardinals is trying to enumerate the one two three four diverse chief weapons of the Inquisition.

Hungarian

"My hovercraft is full of eels" obviously needs no explanation; what might need an explanation is why translating from Hungarian would be relevant in programming.

Hungarian notation, named for the famously Hungarian Xerox/Microsoft employee Charles Simonyi, means encoding the type of a variable into its name. For example, instead of having a variable "name" or "pos", you'd have "szName", which tells you that it's a zero-terminated string, or "rwPos", which tells you that it's a row rather than column position. Relatedly, in Perl and related languages, instead of "name" or "names" you'd have "$name", which tells you that it's a scalar, and "%names", which tells you that it's a hash (dictionary). In Python, none of this is considered idiomatic. If you need separate row and column positions, go ahead and call them "row_pos" and "col_pos", but don't try to come up with a standard "rw" abbreviation and apply it to all row variables whether needed for disambiguation or not. So, if you've translated sample code from (usually) Windows-specific C++ or Visual Basic to Python, and come up with a bunch of variable names like "szName", you've mistranslated from Hungarian.

This is usually a sign of a bigger and more general problem: Translating code at the line-by-line level from a very different language is almost always going to give you non-idiomatic, inefficient, unmaintainable code. This actually comes up more often nowadays with people trying to write Java code, or occasionally even Scheme/OCaml/Haskell code, in Python. But it's the same problem.

"My hovercraft is full of eels" is shorthand for both of these ideas. If someone says that as a comment on your code, it means your code doesn't look like Python.
0

Add a comment

It's been more than a decade since Typical Programmer Greg Jorgensen taught the word about Abject-Oriented Programming.

Much of what he said still applies, but other things have changed. Languages in the Abject-Oriented space have been borrowing ideas from another paradigm entirely—and then everyone realized that languages like Python, Ruby, and JavaScript had been doing it for years and just hadn't noticed (because these languages do not require you to declare what you're doing, or even to know what you're doing). Meanwhile, new hybrid languages borrow freely from both paradigms.

This other paradigm—which is actually older, but was largely constrained to university basements until recent years—is called Functional Addiction.
5

I haven't posted anything new in a couple years (partly because I attempted to move to a different blogging platform where I could write everything in markdown instead of HTML but got frustrated—which I may attempt again), but I've had a few private comments and emails on some of the old posts, so I decided to do some followups.

A couple years ago, I wrote a blog post on greenlets, threads, and processes.
6

Looking before you leap

Python is a duck-typed language, and one where you usually trust EAFP ("Easier to Ask Forgiveness than Permission") over LBYL ("Look Before You Leap"). In Java or C#, you need "interfaces" all over the place; you can't pass something to a function unless it's an instance of a type that implements that interface; in Python, as long as your object has the methods and other attributes that the function needs, no matter what type it is, everything is good.
1

Background

Currently, CPython’s internal bytecode format stores instructions with no args as 1 byte, instructions with small args as 3 bytes, and instructions with large args as 6 bytes (actually, a 3-byte EXTENDED_ARG followed by a 3-byte real instruction). While bytecode is implementation-specific, many other implementations (PyPy, MicroPython, …) use CPython’s bytecode format, or variations on it.

Python exposes as much of this as possible to user code.
6

If you want to skip all the tl;dr and cut to the chase, jump to Concrete Proposal.

Why can’t we write list.len()? Dunder methods C++ Python Locals What raises on failure? Method objects What about set and delete? Data members Namespaces Bytecode details Lookup overrides Introspection C API Concrete proposal CPython Analysis

Why can’t we write list.len()?

Python is an OO language. To reverse a list, you call lst.reverse(); to search a list for an element, you call lst.index().
8

Many people, when they first discover the heapq module, have two questions:

Why does it define a bunch of functions instead of a container type? Why don't those functions take a key or reverse parameter, like all the other sorting-related stuff in Python? Why not a type?

At the abstract level, it's often easier to think of heaps as an algorithm rather than a data structure.
1

Currently, in CPython, if you want to process bytecode, either in C or in Python, it’s pretty complicated.

The built-in peephole optimizer has to do extra work fixing up jump targets and the line-number table, and just punts on many cases because they’re too hard to deal with. PEP 511 proposes a mechanism for registering third-party (or possibly stdlib) optimizers, and they’ll all have to do the same kind of work.
3

One common "advanced question" on places like StackOverflow and python-list is "how do I dynamically create a function/method/class/whatever"? The standard answer is: first, some caveats about why you probably don't want to do that, and then an explanation of the various ways to do it when you really do need to.

But really, creating functions, methods, classes, etc. in Python is always already dynamic.

Some cases of "I need a dynamic function" are just "Yeah? And you've already got one".
1

A few years ago, Cesare di Mauro created a project called WPython, a fork of CPython 2.6.4 that “brings many optimizations and refactorings”. The starting point of the project was replacing the bytecode with “wordcode”. However, there were a number of other changes on top of it.

I believe it’s possible that replacing the bytecode with wordcode would be useful on its own.
1

Many languages have a for-each loop. In some, like Python, it’s the only kind of for loop:

for i in range(10): print(i) In most languages, the loop variable is only in scope within the code controlled by the for loop,[1] except in languages that don’t have granular scopes at all, like Python.[2]

So, is that i a variable that gets updated each time through the loop or is it a new constant that gets defined each time through the loop?

Almost every language treats it as a reused variable.
4
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.