Skip to main content

Python auto-binding dict

Project description


A library allowing you to create an auto method-binding dict.

Ever dreamed of writing clean dicts pointing keys to methods? Fear no more!

Mainly used for event handlers, a binding dict consists of a mapping between any events or keys, to their appropriate handler functions within a class. Upon key lookup, the dict will bind the appropriate function to the instance of the class.

For an example:

class Server:
    """Typical server with a small mapping between event handlers and functions"""
    def __init__(self, name): = name

    def on_connect(self, remote_host):
        print(, remote_host)

    def on_connect(self, remote_host):
        print(, remote_host)

    handlers = BDict({NewConnectionEvent: on_connect,
                      DisconnectonEvent: on_disconnect})

>>> s = Server("myserver")
>>> s.handlers[NewConnectionEvent]("")

As you can see, after accessing the handlers dict, and upon key lookup, the dict bound the handler functions to the instance.

BDict also works with classmethods in a clean and fashioned way:

class MyClass:
    """Typical server with a small mapping between event handlers and functions"""            
    def class_handle(cls):

    handlers = BDict({"class_handle": class_handle})

>>> MyClass.handlers["class_handle"]
<bound method MyClass.class_handle of <class '__main__.MyClass'>>
>>> MyClass.handlers["class_handle"]()

>>> inst = MyClass()
>>> inst.handlers["class_handle"]
<bound method MyClass.class_handle of <class '__main__.MyClass'>>
>>> inst.handlers["class_handle"]()

Upon accessing the BDict through an instance, the BDict will create an internal instance data container on the BDict, allowing you to modify it's dictionary and not affect other instances!

>>> inst.handlers[123] = 456
>>> inst.handlers[123]
>>> inst2 = MyClass()
>>> inst2.handlers[123]
Traceback (most recent call last):
KeyError: 123



dict_ can be a dict or an iterable of (key, value) pairs and will be used to initialize BDict.

The class BDict is used on must support weak referencing (the vast majority of custom objects do). If you use __slots__, you are able to do this by adding __weakref__ to the slots.

BDict.autobind(key, value)

Allows adding an autobinding entry in the dict (regular additions will not autobind). key is the key for access while value is the function that will be autobound.

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 bdict, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size bdict-0.1.0-py3-none-any.whl (4.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size bdict-0.1.0.tar.gz (3.9 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