January 20, 2005
Special Cases

Very interesting London Java Meetup last night. It was nice to see Darren and Steve along for the first time, and I had a very interesting chat with James about Sun's new interest in dynamic languages, and Groovy, his baby. (Jez is also working hard on Groovy at the moment.)

It's interesting to see the differences and similarities between the philosophies of Groovy and Python. One thing that James and (channelling) Guido agree on is that simplicity is good. They disagree on what constitutes simplicity, though.

Take functions for a moment. In both Python and Groovy, functions and methods are 'first class', which means that they are objects in their own right, and can be passed around like any other object. They differ in how that's done, though.

In Python, a function is called by putting parenthesises after its name:

>>> def spam():
...  print "spam's been called!"
>>> spam()
spam's been called!

If you want to refer the function object, you just leave the parenthesises off:

>>> spam
<function spam at 0x0117CDB0>

This makes it easy to pass the function around:

>>> eggs = spam
>>> eggs()
spam's been called!
>>> def chips(a_function):
...  a_function()
>>> chips(spam)
spam's been called!

To me, that's nice and simple. No special cases. As Tim put it, "special cases aren't special enough to break the rules". A function is an object, names are bound to objects, using the base name refers to the bound object.

"Ah!", says James, "but 99% of the time when a newbie leaves the parenthesises off, it's a mistake. That want to call the function." This is probably true. So in Groovy, if the function takes no arguments, you can call it without parenthesises. If you want to refer to the function object, there's a special syntax for that. (Perhaps James or Jez would care to give an example here?) The 'normal' case gets the normal syntax, and the unusual case gets the special syntax. So, is this simpler?

Another example - self. Python requires all methods to take an explicit self argument. After all, a method is just a function belonging to an instance really, and it's simpler to pass that instance explicitly. No black magic, and it makes injecting new methods into classes and instances at runtime simpler. But you always need a reference to the instance, though, so in Groovy it's is available implicitly. It's not like you need to dynamically add methods that often, is it?

To me, having all this implicit stuff is more complex than doing everything explicitly. But to James, keeping the common case simple to write is simpler. Who's right? Who knows. I like Python's approach myself, but it's not obvious to me that James is wrong.

I'm also sad to say that James is starting to understand what it must be like to be Guido, attacked every time you propose a change. Bloody nice chaps, both of them, so this is a real shame.

Oh and where were you, Sam?

Posted to Python by Simon Brunning at January 20, 2005 01:52 PM
Post a comment

Email Address:



Remember info?