Skip to main content

A simple Python package for defaultable abstract base classes

Project description

🧩 Defaultable

Defaultable is a lightweight Python package that provides abstract base classes for defining "default" instances in your own classes — useful for fallbacks, comparisons, initialization patterns, or clean APIs.

It supports both:

  • ✅ Public default behavior via Defaultable
  • 🔒 Internal default behavior via InternalDefaultable

🚀 Installation

Install the package from PyPI:

pip install defaultable

📦 Usage Examples

🔹 Public: Using Defaultable

Use the Defaultable base class when you want to expose a public default instance for your class.

from defaultable import Defaultable
 
class ComplexNumber(Defaultable):
    def __init__(self, re: float, im: float):
        self.re = re
        self.im = im
 
    @classmethod
    def default(cls):
        return cls(1.0, 0.0)  # Default is 1 + 0j
 
    def __eq__(self, other):
        return isinstance(other, ComplexNumber) and self.re == other.re and self.im == other.im
 
    def __repr__(self):
        return f"({self.re} + {self.im}j)

Usage:

>>> c = ComplexNumber.default()
>>> print(c)
(1.0 + 0.0j)
 
>>> ComplexNumber.is_default(c)
True

🔸 Internal: Using InternalDefaultable

Use the InternalDefaultable base class when you need internal logic for a default instance, without exposing it to the public API.

from defaultable import InternalDefaultable
 
class InternalComplex(InternalDefaultable):
    def __init__(self, re: float, im: float):
        self.re = re
        self.im = im
 
    @classmethod
    def _default(cls):
        return cls(0.0, 0.0)  # Internal default is 0 + 0j
 
    def __eq__(self, other):
        return isinstance(other, InternalComplex) and self.re == other.re and self.im == other.im
 
    def __repr__(self):
        return f"({self.re} + {self.im}j)

Internal usage:

>>> default_ic = InternalComplex._default()
>>> InternalComplex._is_default(default_ic)
True

🔒 InternalDefaultable is designed for internal/private use. Its methods are prefixed with underscores and should not be exposed in public APIs.


📄 License

This project is licensed under the MIT License.


🛠️ Contributing

Contributions are welcome! Feel free to open issues or pull requests on GitHub.


🔗 Links

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

defaultable-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

defaultable-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file defaultable-0.1.0.tar.gz.

File metadata

  • Download URL: defaultable-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for defaultable-0.1.0.tar.gz
Algorithm Hash digest
SHA256 327b17c6c30af0c32a325b8c508011335d88c99c262b8c382c3e7455717c4e3f
MD5 72686450cdba603ffa2a187b0d1d835d
BLAKE2b-256 b8d6c6f7632431150d56e0045cfc685117d0f6a32c7381c2e6423c10748c1c99

See more details on using hashes here.

File details

Details for the file defaultable-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: defaultable-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for defaultable-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fb7e86311d7645a9ae3a04112bc5bb9c8cddd76641510786ae79a7045893352
MD5 d5e4321146251986dd21841848528081
BLAKE2b-256 8e72f8905de335f73caba24e9a276b242af97bb931767133aaaa20e11cb90485

See more details on using hashes here.

Supported by

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