Sunday, July 23, 2006

Re: One Array to rule them all

Pretty late reply... I've been doing a bit of python of late. And although there isn't 'one array to rule them all' there are some similarities. In python there are three main data structures - lists, dictionaries and tuples. Lists are growable arrays (ArrayList in Java) and Dictionaries are associative arrays (HashMap in Java). Tuples are just containters to hold other objects (No equivalent in Java). They are immutable.

People always talk of productivity with dynamic languages. These built-in data structures are one of the main reasons for it. Sure Java has an amazing collections framework, but it's not a language feature. It's a framework built on top of the language. In python, these are part of the language... [] creates a list, {} creates a dictionary and () creates a tuple.

Having these be part of the language makes a huge difference. In Java, if you want to return multiple values from a method... either do something ugly like return an array with the values (the values have to be of the same type) or define a new class to hold the values and return an instance of that class. Quite a pain. In python, you just put the values (of any type) inside a tuple and return. As simple as that. And as you can imagine, lists are used a lot to hold collections of data. But probably the most used data structure is the dictionary. It's a dumping ground in all sorts of situations. You would think it would be a readability nightmare but it isn't. It's almost a convention in python and when you come across code, it is immediately recognizable. And these ds's combined make for more productivity gains. Tuples, being immutable, can be used as keys. Also, python has support for sets as well.

No comments: