Friday, January 15, 2010

Getting to Love Python

So I ran into a bit of struggle last night/this morning. I found some code that took a stream of data and wrote it out to a file. Unfortunately, so I believed, this wouldn't work:

f = open("/tmp/foo")
s = getsomestreamofbinarydata()
f.write(s)
This shouldn't work for the simple reason that strings are not binary. Strings are essentially char *, right? References to arbitrary-length arrays of characters. Oh, sure, they could be unicode, but they can't be binary, right?

Wrong. The code above works, because python doesn't really give a damn about your data until you try to do something with it, and then it does its honest best to do typing/transformation as necessary.

Now, the problem is that this isn't well-documented. Why not? Perhaps it is because the "feature" is just python; it is just assumed by python programmers to work. And for more naive programmers, a smart typing system is a very, very good thing. Also, there are downsides to "smart" typing; when it works, it is just smart typing, but when it doesn't work, it is "smart" typing. There are good uses for scare quotes.

I do like typed languages, even strongly typed languages; they have their value. I miss overloading methods (yes, you sort-of can do it, but not really). The best method overloading, btw, is in dylan. Too bad, Apple could have had the best core development language, and now they have perhaps the second best.

No comments:

Post a Comment