13 November 2007

Python dictionaries and Java Hashtables Analogy

I stumbled across the open source SAGE project this past week. Among many other things, SAGE uses Python to unify the many disparate programming languages introduced by packages like Mathematica and MATLAB. I figured SAGE would finally be a good excuse to improve my lousy Python skills, and I started reading Dive Into Python.

During it's description of Python dictionaries, Dive Into Python makes the comment that

"A dictionary in Python is like an instance of the Hashtable class in Java."
Immediately I've got some bad lingering associations from my former IBM server-side Java work. Hashtable access is inherently synchronized in Java, but many programmers aren't aware that they're paying synchronization overhead on every get and put. These folks learned about Hashtables from Java 1.1 material and never moved onward to Maps as the Collections framework improved. Scarier still is when people do know about Hashtable synchronization but fail to use newer mechanisms like Collections.synchronizedMap wrappers and ConcurrentMap implementers to document their synchronization needs explicitly.

All in all, Dive Into Python's analogy seemed wrong. So I bugged Michael Gilfix. Mike's my single source for all questions involving at least two of the trio "Java/Python/threads":

Hey Mike,

I ran across
  > A dictionary in Python is like an instance of the Hashtable class in Java.
in a python tutorial I was skimming.  Do they really mean Hashtable
with all the underlying synchronization implications?  Or do they mean
HashMap and didn't think hard enough about the analogy?

Thanks,
Rhys

To which he replied:

Hey. It's like a hashmap. You need to synchronize using something like
mutexes from the threading package. None of the built in collection types
come with built in synchronization. Sounds like you're having fun.

No comments:

Subscribe Subscribe to The Return of Agent Zlerich