Lazy attribute
==============
The lazy decorator defines an attribute with deferred initialization::
.. code::python
import math
from lazyutils import lazy
class Vec:
def __init__(self, x, y):
self.x, self.y = x, y
@lazy
def magnitude(self):
print('computing...')
return math.sqrt(self.x**2 + self.y**2)
Now the ``magnitude`` attribute is initialized and cached upon first use:
>>> v = Vec(3, 4)
>>> v.magnitude
computing...
5.0
The attribute is writable and apart from the deferred initialization, it behaves
just like any regular Python attribute.
>>> v.magnitude = 42
>>> v.magnitude
42
Lazy attributes can be useful either to simplify the implementation of the
__init__ method of objects that initialize a great number or variables or as an
optimization that delays potentially expensive computations that may not be
necessary in the object's lifecycle.
Delegation
==========
The delegate_to() function delegates some attribute to an attribute during the
class definition::
.. code::python
from lazyutils import delegate_to
class Arrow:
magnitude = delegate_to('vector')
def __init__(self, vector, start=Vec(0, 0)):
self.vector = vector
self.start = start
Now, the ``.magnitude`` attribute of ``Arrow`` instances is delegated to
``.vector.magnitude``. Delegate fields are useful in class composition when one
wants to expose a few selected attributes from the inner objects. delegate_to()
handles attributes and methods with no distinction.
>>> a = Arrow(Vec(6, 8))
>>> a.magnitude
computing...
10.0
Aliasing
========
Aliasing is a very simple form of delegation. We can create simple aliases for
attributes using the alias() and readonly() functions::
class MyArrow(Arrow):
abs_value = readonly('magnitude')
origin = alias('start')
This exposes two additional properties: "abs_value" and "origin". The first is
just a read-only view on the "magnitude" property. The second exposes read and
write access to the "start" attribute.
Metadata
Release files for lazyutils 0.3.3
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 |
|---|---|---|---|---|
| lazyutils-0.3.3.tar.gz | 6.9 kB | Without provenance | Details for lazyutils-0.3.3.tar.gz |
Release files / lazyutils-0.3.3.tar.gz
| Download URL | lazyutils-0.3.3.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
18c246854e257d6843b12e3b43cd00fadfb547ba51cdb80f4d14d9ccaa5aa444
|
|
BLAKE2b-256 checksum How to use checksums |
de23c7ebd89f7b75f1f95b4dd5e9e77d4d4761d694e9b0ad61b17fda8e57007c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for lazyutils 0.3.3
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.