--- author: '' category: '' date: 2013/03/08 18:13:41 description: It's a dictionary! It's a function! It's a Fundict! link: '' priority: '' slug: python-trick-the-fundict-or-the-diction tags: python title: 'Python Trick: the Fundict (or the Diction)' type: text updated: 2013/03/08 18:13:41 url_type: '' --- Suppose you have made the choice in the past of exposing a dictionary as part of an object's interface. So, people are doing things like:: object.data[foo]['bar'] And now you want people to not have to specify ``foo`` because it can be obtained from somewhere else. In fact, what you want now is to expose something like this:: object.data(bar, foo='foo') Here's an idea on how to do that *without breaking the old code*: .. code:: python class fundict(dict): def __call__(self, bar, foo='foo'): return self[foo][bar] That's a dictionary that's also a callable, and thus indistinguishable from a function. A function-dictionary. A fundict. And of couse, you could also do it the other way around, and implement a function that works as a dictionary. A dictionary-function, a diction. But since that's more work, I used this one.