Skip to main content

TypeDispatch is a Python utility for registering and dispatching functions based on object types, supporting inheritance through method resolution order (MRO).

Project description

typedispatch

typedispatch is a Python library for registering functions based on type resolution. It allows you to associate types with functions and dynamically look up functions based on the type of an object, including handling inheritance hierarchies.

Installation

pip install typedispatch

Usage

Basic Registration

You can register functions directly with the register method:

from typedispatch import TypeDispatch, TypeDispatchError

typedispatch = TypeDispatch()

typedispatch.register(int, lambda x: f"Processing integer: {x}")
print(typedispatch.lookup(10))  # Output: Processing integer: 10

Using the Decorator

The library also provides a convenient decorator for registering functions with specific types. This makes the code cleaner and easier to manage.

from typedispatch import TypeDispatch

typedispatch = TypeDispatch()

@typedispatch.register_decorator(int)
def handle_integer(x):
    return f"Handling integer: {x}"

@typedispatch.register_decorator(str)
def handle_string(x):
    return f"Handling string: {x}"

print(typedispatch.lookup(42))        # Output: Handling integer: 42
print(typedispatch.lookup("hello"))   # Output: Handling string: hello

Project details


Download files

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

Source Distribution

typedispatch-0.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

typedispatch-0.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file typedispatch-0.2.0.tar.gz.

File metadata

  • Download URL: typedispatch-0.2.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for typedispatch-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7e4a6490a94a9d233ccce2824b353392287095e5404880647f0a4d9d2ab4cb8e
MD5 fab9caf387de41ba336fe4a0155d640d
BLAKE2b-256 f4b03a4f3ba8088a491b74ed1bc4def61b8f6e922121f1d70c2618fe8865de68

See more details on using hashes here.

File details

Details for the file typedispatch-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typedispatch-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d20ccfdfb02cd72a5207398d3262cc5dd3af096cdb06d9b42fbf1b91c2001da
MD5 b96249c5bf8ca5e704ae3831bd98087c
BLAKE2b-256 a62925789ca655ce81c5a1c9c8270ca3e5654dec71079c1ffd2d736b00aa690f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page