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 Complex(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 __bool__(self):
default_complex = self._default()
return self.im != default_complex.im or self.re != default_complex.re
Internal usage:
>>> my_complex = Complex(0, 0)
>>> bool(my_complex)
False
🔒
InternalDefaultableis 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file defaultable-0.1.1.tar.gz.
File metadata
- Download URL: defaultable-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc0126021ce2e3d79fd731705d58483fe0f9e294f52f03ebc4c54386b57e6d86
|
|
| MD5 |
a8543edc9652a7dcc68530e2068399b0
|
|
| BLAKE2b-256 |
d7609c2c34c6ba780f14d1be4a7630456e92ceb66a35c2b736c196bbf236faa6
|
File details
Details for the file defaultable-0.1.1-py3-none-any.whl.
File metadata
- Download URL: defaultable-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
664dbdac97d7671c74fb6fc6e0b0916d5cebab2603b4ec0b15d4b234a74d7e32
|
|
| MD5 |
d19a5815c8e0e91c29133e9d3a33602b
|
|
| BLAKE2b-256 |
d61c7310e4c21e3b933e914c9dee904d7f419ffe53256c323b756d4a46c0adca
|