Skip to main content

Memoized dictionaries

Project description


Dictionaries that enable the lazy evaluation and memoization of callable entries. thunk-dict is meant to serve as a convenient wrapper the lazy evaluation of potentially computationally expensive calls.

Use case

Because Python eagerly evaluates expressions, it is not possible include calls to functions within a dictionary without evaluating those calls first. To mimic lazy evaluation, one may turn to tricks like:

dictionary = {
  "key1": computationally_expensive_function

# Call the function later on
result = dictionary["key1"]()

or alternatively, if you also wanted to pass in arguments later on,

dictionary = {
  "key1": lambda *args, **kwargs: computationally_expensive_function(*args, **kwargs)

# Call the function later on
result = dictionary["key1"]("blah", parameter=100)

However, this approach suffers from two issues:

  1. It complicates the typing of your dictionaries and its entries and creates boilerplate (as in the second example).
  2. Reaccess requires the computationally expensive functions to be called again (i.e. the call isn't memoized)

thunk-dict attempts to resolve both of these issues.

from thunk_dict import ThunkDict
dictionary = ThunkDict({
  "key1": computationally_expensive_function

# Get result from call via simple access
result = dictionary["key1"]

# Subsequent accesses use the memoized result.

For noncallable entries, thunk-dict works just like a regular dict. thunk-dict also features the same API as regular Python dictionaries, meaning you don't have to sacrifice anything by using its wrapper.


  1. Convenient thunk-like behavior in dictionaries
  2. Works for any objects that register with dict
  3. Very low overhead


From PyPi

pip install thunk-dict

From source distribution

git clone
pip install .

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for thunk-dict, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size thunk_dict-0.1.1-py3-none-any.whl (3.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size thunk_dict-0.1.1.tar.gz (2.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page