Archive for February, 2014

classy enumerations

2014-02-17

An enumeration is a term that usually refers to a type consisting of a finite number of distinct members. The members themselves can be tested for equality, but usually their particular value is not important.

Maybe you’re modelling a Sphex wasp and you have a state variable with values NOTNEARHOME, JUSTLEFTHOME. You could represent that with an enumeration.

In C the enum keyword assigns (usually small) integers to constant identifiers. It is problematic, chiefly because the members of the enumeration are really just integers. After enum { Foo; }, then code like Foo / 5 is valid (note: valid, not sensible).

In Python you could do essentially the same thing:

NOTNEARHOME = 0
JUSTLEFTHOME = 1

if self.state == NOTNEARHOME:
    if 'spider' in self.inventory:
        # head towards home
    else:
        # look for juicy spiders

You do see this style (ast.PyCF_ONLY_AST Note 1), but it has the same problems as enum in C. The values are just integers (so, for example, print self.state will print 0, or 1).

You could use strings (like decimal.ROUND_HALF_EVEN):

NOTNEARHOME = 'notnearhome'
# and so on...

That’s better because now I might have a clue if a print out a value and it’s 'notnearhome', but it’s only a little bit better, because you still might accidentally use the value innappropriately (opt = decimal.ROUND_HALF_EVEN; opt += 'foo').

I have a proposal:

Use a class!

class NOTNEARHOME: pass         # Note 2
class JUSTLEFTHOME: pass

Let’s call this classy enumerations.

Classy enumerations have the advantage that we don’t need to manually assign numbers or strings. Values like Mood.PUZZLED and Mood.CONFUSED (which are actually classes) will be unique, so can be tested using == or is correctly.

With classy enumerations we get an error if we accidentally use them in an expression:

>>> PUZZLED+1
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'classobj' and 'int'

And to wrap up:

class True: pass
class False: pass

This article was inspired by looking at some of Peter Waller‘s code who seems to have invented the idea of using classes for enumerations.

Note 1

Yes this value matches a value in the C header file. Maybe that has some merit, but it doesn’t make for a very Pythonic interface.

Note 2

The body of a class definition is just a block of code. When that body is just a simple statement, it can share the line with the class declaration. Hence, class NOTNEARHOME: pass is a compact complete class definition. If you’re in a mood for documentation, replace “pass” with a docstring.

Advertisements

Teach everyone to program

2014-02-05

The microcomputer was invented only a generation ago, it is now in hundreds of devices and gadgets in every home.

We are on the brink of a revolution. A revolution as significant as the printing press. It was hundreds of years between the press and universal literacy (still not quite achieved in the UK, our adult literacy rate is 99%), but we now live in a society where so much information is written and so much commerce and social interaction takes place in writing that you are at a severe disadvantage if you cannot read and write. We are not all writers, but we can all write. Poets can move entire Nations with carefully crafted written words, but even if most of us can’t achieve that, we can at least write a note to our milkman asking for 2 more pints on Saturday (Note 1).

Imagine if Gove (Note 2) suggests that we only teach the gifted to write. Only the playwrights, the speechwriters, the journalists, and the poets will write. After all, all the good stuff is written by them anyway. I hope you can see that this would be madness.

I feel the same way about code and programming. There is a poetry to code; the poets of programming write code with concision and precision: the structure writ clear on the fan-folded page. There is already a rich literature of programming. github overflows with the pulp fiction of the professional and amateur hack alike; likely we will find Lovecraftian horrors there too, lurking, ready to turn our minds into a pretzel. But at the moment this culture is the culture of an elite class. Everyone should be able to program. Most people will not be poets. Most people will not be programmers, but that should not stop us from teaching them to program.

In the future code will be woven into the fabric of our society, just as the written word is woven now. We don’t teach people to read and write because it will be helpful to them in their future career. We teach them because it is inconceivable that they can function without basic literacy. It is inconceivable that in the future we will be able to function without basic coding.

This is why I’m excited about teaching kids to program. Lots of grassroots initiatives to teach programming, like Software Carpentry, Raspberry Pi, Young Rewired State. Let’s build the future now. Teach everyone to program.

Note 1

I live in a quaint postcode where I can still get milk delivered to my doorstep. And I do so.

Note 2

Hello readers from the future! Gove was responsible for education policy in the United Kingdom (Note 3) for a brief period in the early 21st Century.

Note 3

Hello readers from the more distant future. The United Kingdom was a nation consisting of various bits of islands in an archipelago off the North West coast of Europe.