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.

A Functional Addict is someone who regularly gets higher-order—sometimes they may even exhibit dependent types—but still manages to retain a job.

Retaining a job is of course the goal of all programming. This is why some of these new hybrid languages, like Rust, check all borrowing, from both paradigms, so extensively that you can make regular progress for months without ever successfully compiling your code, and your managers will appreciate that progress. After all, once it does compile, it will definitely work.

Closures

It's long been known that Closures are dual to Encapsulation.

As Abject-Oriented Programming explained, Encapsulation involves making all of your variables public, and ideally global, to let the rest of the code decide what should and shouldn't be private.

Closures, by contrast, are a way of referring to variables from outer scopes. And there is no scope more outer than global.

Immutability

One of the reasons Functional Addiction has become popular in recent years is that to truly take advantage of multi-core systems, you need immutable data, sometimes also called persistent data.

Instead of mutating a function to fix a bug, you should always make a new copy of that function. For example:

function getCustName(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

When you discover that you actually wanted fields 2 and 3 rather than 1 and 2, it might be tempting to mutate the state of this function. But doing so is dangerous. The right answer is to make a copy, and then try to remember to use the copy instead of the original:

function getCustName(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

function getCustName2(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[2] + ' ' + custRec[3];
    return fullname;
}

This means anyone still using the original function can continue to reference the old code, but as soon as it's no longer needed, it will be automatically garbage collected. (Automatic garbage collection isn't free, but it can be outsourced cheaply.)

Higher-Order Functions

In traditional Abject-Oriented Programming, you are required to give each function a name. But over time, the name of the function may drift away from what it actually does, making it as misleading as comments. Experience has shown that people will only keep once copy of their information up to date, and the CHANGES.TXT file is the right place for that.

Higher-Order Functions can solve this problem:

function []Functions = [
    lambda(custID) {
        custRec = readFromDB("customer", custID);
        fullname = custRec[1] + ' ' + custRec[2];
        return fullname;
    },
    lambda(custID) {
        custRec = readFromDB("customer", custID);
        fullname = custRec[2] + ' ' + custRec[3];
        return fullname;
    },
]

Now you can refer to this functions by order, so there's no need for names.

Parametric Polymorphism

Traditional languages offer Abject-Oriented Polymorphism and Ad-Hoc Polymorphism (also known as Overloading), but better languages also offer Parametric Polymorphism.

The key to Parametric Polymorphism is that the type of the output can be determined from the type of the inputs via Algebra. For example:

function getCustData(custId, x)
{
    if (x == int(x)) {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return int(fullname);
    } else if (x.real == 0) {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return double(fullname);
    } else {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return complex(fullname);
    }
}

Notice that we've called the variable x. This is how you know you're using Algebraic Data Types. The names y, z, and sometimes w are also Algebraic.

Type Inference

Languages that enable Functional Addiction often feature Type Inference. This means that the compiler can infer your typing without you having to be explicit:


function getCustName(custID)
{
    // WARNING: Make sure the DB is locked here or
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

We didn't specify what will happen if the DB is not locked. And that's fine, because the compiler will figure it out and insert code that corrupts the data, without us needing to tell it to!

By contrast, most Abject-Oriented languages are either nominally typed—meaning that you give names to all of your types instead of meanings—or dynamically typed—meaning that your variables are all unique individuals that can accomplish anything if they try.

Memoization

Memoization means caching the results of a function call:

function getCustName(custID)
{
    if (custID == 3) { return "John Smith"; }
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

Non-Strictness

Non-Strictness is often confused with Laziness, but in fact Laziness is just one kind of Non-Strictness. Here's an example that compares two different forms of Non-Strictness:

/****************************************
*
* TO DO:
*
* get tax rate for the customer state
* eventually from some table
*
****************************************/
// function lazyTaxRate(custId) {}

function callByNameTextRate(custId)
{
    /****************************************
    *
    * TO DO:
    *
    * get tax rate for the customer state
    * eventually from some table
    *
    ****************************************/
}

Both are Non-Strict, but the second one forces the compiler to actually compile the function just so we can Call it By Name. This causes code bloat. The Lazy version will be smaller and faster. Plus, Lazy programming allows us to create infinite recursion without making the program hang:

/****************************************
*
* TO DO:
*
* get tax rate for the customer state
* eventually from some table
*
****************************************/
// function lazyTaxRateRecursive(custId) { lazyTaxRateRecursive(custId); }

Laziness is often combined with Memoization:

function getCustName(custID)
{
    // if (custID == 3) { return "John Smith"; }
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

Outside the world of Functional Addicts, this same technique is often called Test-Driven Development. If enough tests can be embedded in the code to achieve 100% coverage, or at least a decent amount, your code is guaranteed to be safe. But because the tests are not compiled and executed in the normal run, or indeed ever, they don't affect performance or correctness.

Conclusion

Many people claim that the days of Abject-Oriented Programming are over. But this is pure hype. Functional Addiction and Abject Orientation are not actually at odds with each other, but instead complement each other.
5

View comments

Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.