Archive for October, 2008

Python’s enigmatic self

The old ‘self’ controversy is making another appearance, stirred up by Bruce Eckel’s proposal to remove the requirement.

Let’s start with a quick summary of the controversy.

In Python, you have to declare “self” as a first parameter of all your methods:

[python]
class Foo():
def baz(self, x):
pass
[/python]

This is just a convention. The name “self” is not enforced by the compiler but it’s necessary to declare it as the first parameter of your method or Python will consider your method to be a function.

Python programmers feel strangely strongly about this convention and most of them don’t see this requirement as a problem (which is the reason why there is so much misunderstanding between both camps).

Interestingly, both sides often seem to miss the main reason why this controversy exists in the first place. It is *not* about typing less characters, it is *not* about having to prefix fields with that name (most people are fine with typing “self.f = 42”).

This is strictly about consistency, and James Watson summarized the problem very cleanly in his comment on Bruce Eckel’s proposal:

I think a lot of people are bothered by this because there is a disconnect between the number of arguments in the declaration and the number of arguments in the call.

In short: the method ‘baz’ above is declared with two parameters in the class but you invoke it with only one.

This problem goes beyond Python and OO philosophies: it’s the violation of a fundamental concept that has been the foundation of programming languages going decades back: when you declare a function with n parameters, you should invoke it with n parameters (slight exception if some of these parameters are optional, but this aspect is not germane to the discussion).

Guido took some time to explain why Bruce’s proposal cannot work. This is not the first time that explicit self has been under attack, and Guido has defended his design many times over the years.

I went over Guido’s explanation several times and to be honest, my eyes glazed over. My take away is that the reasons for the current design seem to be pretty serious and they dive deep into Python’s implementation.

And that’s mostly why I keep being bothered by explicit self: it seems to me it’s a design that is dictated more by the implementation than by the will to make this part of the language intuitive and in line with the estalbished science of computer languages.

C++, Java, C# and many other object-oriented languages support the concept of a self object available within the scope of the current method, and none of them require you to declare that parameter explicitly, so it can obviously be done. Arguing that it’s cleaner to require that parameter for the sake of readability when the real reason seems to be a limitation in the implementation strikes me as a bit dishonest.

To make matter worse (or better, if you’re into that sort of thing), Python has been adding dynamic features over these past years (such as adding a method inside a class outside the declaration of that class) that pretty much mean that the explicit self will never go away. I’m not convinced that these dynamic features were worth closing the door on the possibility of ever resolving the explicit self controversy, but the damage is done, and at this point, it’s pretty clear that Python will never lose its explicit self.

Android Translate

My modest contribution to the ever-growing Android Market is Translate, an application that uses Google’s back-end to translate over 150 language pairs. I wrote it pretty quickly, just as a proof of concept and also to see how it feels to use the “real” (public) SDK, as opposed to the internal builds that I’ve been using these past two years.

Considering the simple scope of this application, slapping a simple UI on top of some HTTP exchanges with the Google Translate API was fairly trivial. Translate was downloaded more than 10,000 times so far and the comments are very positive (and sometimes downright hilarious).

The most surprising part to me is how few users seem to realize that Translate actually uses Google Services to do its job, and then it occurred to me that it’s one of the first times where the code that I write actually gets used by non-technical people (with the exception of Gmail for Java ME and Gmail for Android, which also taught me a lot about our audience).

Before I uploaded the application to Android Market, I also decided to make it easy for users to reach me, so not only did I leave my email address in clear text, but I made it possible to send me an email pretty much with just one button. I thought it was a bit risky since I could find myself flooded with emails, but I
decided that Gmail would make it easy for me to filter out such emails anyway.

I have received over fifty emails so far, and most of them are to say how much they like the application and are also asking for additional features, such as:

  • More languages.
  • Make it possible to send the translation by email or SMS.
  • Allow the history to be cleared.
  • Hook it up to other applications so that users can, for example,
    translate emails they receive.

I’ll be addressing most of these requests in the next version, but in the meantime, I’m learning a lot from this little social experiment. Oh, and if you are curious or you wish to contribute to this application (and a bunch of others), you can find its source on Google Code.

The Android invasion begins

A newcomer in the Android family. Putting this droid together was no small feat.

Disappointed in Alienware

A few weeks ago, I decided to upgrade my two year old PC. It was
still serving me well and it was running World of Warcraft fine, but
I thought that two years
was long enough in hardware time to warrant an upgrade. So far, I had always bought my
PC’s from Dell but this time, I decided to go more hardcore and order
one from Alienware.

My first disappointment was that it took much longer for them to ship
it than my initial reading of the web site led me to believe.
I thought I would get my new computer in about five
days, but after that time, my payment hadn’t even been processed yet.
After that, it took more days assemble it, then run the tests and
finally ship it. Overall, two weeks separated the time where I
confirmed my order online and the time where it was delivered to my
home.

Unfortunately, my new PC never worked properly: after one or two hours, it would completely lock up and I had to cold
reboot it. And because of that, Vista didn’t get a chance to dump any
kind of meaningful information, so viewing the Event Log produced
absolutely no clue about what was happening. This was absolutely
maddening.

I contacted Alienware’s Technical support and a long email dance
started. They started by offering me generic advice and then became
more and more technical as it turned out that none of their solutions
worked. In the latest stages, they were asking me to update my BIOS
(which voids your warranty) and other crazy things such as changing the
clock timings of my memory. The more I interacted with the technicians,
the more obvious it was to me that they had absolutely no idea what
they were doing and that all these recommendations were actually
complete shots in the dark.

Of course, none of them worked and my system continued to crash every
two hours or so. At the height of my frustration and running out of
options, I decided to start the cancellation process, something I wasn’t
exactly looking forward to since it appeared that not only would I
have to pay the shipping costs (which they charged me
for $170 on the way in) but they also featured a 15% restocking fee (something I
would fight tooth and nail since I really doubt restocking fees apply
to defective units).

I also started doing more research on my side and just as I was about
to finalize the return process, I finally found the miracle fix.

All I needed to do was to go to Vista’s Power Options and check the
“High Performance” option, as shown below:

Once I did this, my computer stopped crashing.

Overall, I find it absolutely unacceptable that a company whose core
business is gaming and high-end PC’s was unable to diagnose such a
simple problem, but what is even worse is that they didn’t even catch
this problem in their hundreds of tests that they claim they subject
the machines to before they ship them to customers.

I’m going to keep this machine since it now works correctly, but this
is definitely the first and the last time that I ever buy something
from Alienware.