Forwardable
Utility for easy object composition via delegation. Roughly ported from Ruby’s forwardable standard library.
Requirements
Python 2.7 or 3.3 w/ standard library. Might work on other version of Python, too.
Installation
$ pip install forwardable
Usage
Most Common Use Case
The @forwardable.forwardable() decorator enables you to use def_delegator() and def_delegators() in a class definition block.
Use def_delegators() to define multiple attr forwarding:
from forwardable import forwardable
@forwardable() # Note the () here, which is required.
class Foo(object):
def_delegators('bar', 'add, __len__')
def __init__(self):
self.bar = set()
foo = Foo()
foo.add(1) # Delegates to foo.bar.add()
assert len(foo) == 1 # Magic methods works, too
Easy, heh?
Define a Single Forwarding
In case you only need to delegate one method to a delegatee, just use def_delegator:
from forwardable import forwardable
@forwardable()
class Foo(object):
def_delegator('bar', '__len__')
def __init__(self):
self.bar = set()
assert len(Foo()) == 0
And it should work just fine. Actually, def_delegators() calls def_delegator() under the hood.
Plucking
from forwardable import forwardable
@forwardable()
class MyDict(object):
def_delegator('dct.get', '__call__')
def __init__(self):
self.dct = {'foo', 42}
d = MyDict()
# Equivlant to d.dct.get('foo')
assert d('foo') == 42
Less Magical Usage
The @forwardable() decorator injects def_delegator{,s} into the module scope temorarily, which is why you don’t have to import them explicitly. This is admittedly magical but discourages the usage of import *. And it’s always nice to type less characters whenever unnecessary.
If you hesitate to utilize this injection magic, just explicitly say from forwardable import def_delegator, def_delegators, use them in a class definition and you’ll be fine.
Links
Source Repository: https://github.com/5long/forwardable
License
MIT license.
Metadata
Release files for forwardable 0.4.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 |
|---|---|---|---|---|
| forwardable-0.4.1.tar.gz | 5.1 kB | Without provenance | Details for forwardable-0.4.1.tar.gz |
Release files / forwardable-0.4.1.tar.gz
| Download URL | forwardable-0.4.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e84abd6aed24b39659304b8765c57fecaec208d372ea287fdbe71409f11139ca
|
|
BLAKE2b-256 checksum How to use checksums |
7401af9252495478b84ecb6a139b67df57da4e32da5670f33028396111a962c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for forwardable 0.4.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.