One of my favorite features of Python are lists and touples. I was eager to read about them in Core Python and not disappointed.
I cannot count how many times I have written linked list implementations. I first did it in Pascal, back when I was in high school. I wrote them again in C in college. I would have never bothered writing them in Java, if it wasn't for interviews. In case you didn't know already, interviewing is a crap shoot
. Everybody thinks they are smart, so when they try to figure out if a candidate is smart or not, they simply measure how similar is to do The Ideal Programmer. Of course The Ideal Programmer is actually ... you. Whoever you are, you probably think you are The Ideal Programmer, so when you interview somebody else you simply try to figure out how similar they are to you. But I digress...
Where was I? Interviews. I have been asked to implement a linked list so many times in interviews. Often the interviewer thinks they are particularly clever and try to sneak in the list:
Interviewer: What if we needed to cache some data because retrieving it from a database is expensive? But our cache is not big enough to hold everything from the database. How would we do that?
Me: Perhaps an LRU.
Interviewer (acting confused): Ohhhhhh, what is that?
(after an academic definition of LRU is given)
Interview: Oh how intriguing. How would the data be stored in the cache?
Me (trying not to yawn): A hashtable.
Interivewer: Oh that's good! But how do we figure out what data to evict from the cache when we run out of room?
Me (struggling to stay awake): A linked list
Interviewer (ecstatic): Oh yes! Could you write a linked list for me?
I really think there are a lot of engineers out there who think that if you can write a linked list, you must be a good programmer. Of course that's because they can write linked lists, and they are a good programmer... Anyways, I'm lucky enough to be at a point in my career that if somebody asked me to write a linked list, I would just laugh at them and leave. No more!
One of the great things about Python is that doesn't insult my intelligence by asking me to write a linked list. Instead it has not only lists, but touples! Being a mathematician at heart, this is especially appealing to me. I didn't realize just how powerful lists were in Python. In particular, Core Python has taught me to appreciate the power of splicing:
>>> airports = ['sjc', 'sfo', 'oak', 'mem', 'atl', 'pfn']
>>> airports[:2] + airports[4:]
['sjc', 'sfo', 'atl', 'pfn']
['sjc', 'oak', 'atl']
>>> print airports
['atl', 'mem', 'oak', 'pfn', 'sfo', 'sjc']
I could go on and on playing with this. Negative indexes are great. Steps are great. Being able to use + for concatenation is great. If you've programming in Python for a long time, this all seems mundane I'm sure. If you are a Java or C++ guy ... well maybe you probably appreciate it at least a little. Now in all fairness, Ruby's arrays are pretty powerful too, and Scala's lists and touples are very similar to Python. I need to go and try some of the Python sugar out in those languages...