autoinit
Python decorator for automatic initialization instance attributes
What
import autoinit # same as `from autoinit import autoinit`
@autoinit
class X:
def __init__(self, a, b, c, d:int, e=99.99, f='some_default_value'):
print("__init__ do some another things")
x = X(42, 100, 500, None)
# Output: "__init__ do some another things"
print(x.__dict__)
# Output: {'a': 42, 'b': 100, 'c': 500, 'd': None, 'e': 99.99, 'f': 'some_default_value'}
How
$ pip install autoinit
Where
Tested in:
- CPython: 2.7, 3.5-3.11
- Pypy: 2.7, 3.5-3.9
- Jython: 2.7
...but with a high probability will work with other implementations as well.
Why
A lot of elementary assignments inside __init__ are a fairly frequent and rather dull case.
class FiveDimensionRecord:
def __init__(self, x:int, y:int, z:int, u:int,
v:int, dt:typing.Optional[datetime]=None, description:str=''):
self.x = x
self.y = y
self.z = z
self.u = u
self.v = v
self.dt = dt or datetime.now()
self.description = description
Dataclasses do not make it much more fun, mainly because you still cannot declare attributes in one line
@dataclass
class FiveDimensionRecord:
x: int
y: int
z: int
u: int
v: int
dt: 'typing.Any' = None
description: str = ''
def __post_init__(self):
self.dt = self.dt or datetime.now()
With autoinit it looks much more compact and minimalistic
class FiveDimensionRecord:
@autoinit
def __init__(self, x:int, y:int, z:int,
u:int, v:int, dt=None, description:str=''):
self.dt = self.dt or datetime.now()
Options
-
@autoinit(exclude='attr')or@autoinit(exclude=['attr1', 'attr2]'): skip specified attributes. Default:[] -
@autoinit(no_warn=True): do not throw warning if decorator applied to non-__init__method. Default:False. -
@autoinit(reverse=True): invert the order of actions - first call the wrapped method (which is usually__init__), and then do assignment. Default:False.
The decorator itself can be equally applied to both the __init__ method and the entire class.
Metadata
Release files for autoinit 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| autoinit-1.1.1.tar.gz | 6.3 kB | Without provenance | Details for autoinit-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| autoinit-1.1.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for autoinit-1.1.1-py2.py3-none-any.whl |
Total release size: 11.8 kB
Release files / autoinit-1.1.1.tar.gz
| Download URL | autoinit-1.1.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0c276ca9d5a7962416bc5b8d23e5acb9afad9880c5ec130c2cf4139b2b2f5836
|
|
BLAKE2b-256 checksum How to use checksums |
40f62803316df6781430897abb8016ea1db9762d8e267ca0cb56d756efd5761b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Release files / autoinit-1.1.1-py2.py3-none-any.whl
| Download URL | autoinit-1.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3bd9d74330517a1796f16eb45ce9e5efe3a746030d3bdf78f46b64b58c150493
|
|
BLAKE2b-256 checksum How to use checksums |
50627647028c77e67c2fb75c803dc5a4490707932379e2297abb485075ab6abc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Security for autoinit 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.