Utility for easy object composition via delegation. Roughly ported from Ruby's forwardable standard library.
Python 2.7 or 3.3 w/ standard library. Might work on other version of Python, too.
$ pip install forwardable
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:
fromforwardableimportforwardable@forwardable() # Note the () here, which is required.classFoo(object):
def_delegators('bar', 'add, __len__')
def__init__(self):
self.bar=set()
foo=Foo()
foo.add(1) # Delegates to foo.bar.add()assertlen(foo) ==1# Magic methods works, tooEasy, heh?
In case you only need to delegate one method to a delegatee, just
use def_delegator:
fromforwardableimportforwardable@forwardable()classFoo(object):
def_delegator('bar', '__len__')
def__init__(self):
self.bar=set()
assertlen(Foo()) ==0And it should work just fine. Actually, def_delegators() calls
def_delegator() under the hood.
fromforwardableimportforwardable@forwardable()classMyDict(object):
def_delegator('dct.get', '__call__')
def__init__(self):
self.dct= {'foo', 42}
d=MyDict()
# Equivlant to d.dct.get('foo')assertd('foo') ==42The @forwardable() decorator injects def_delegator{,s} into the
module scope temporarily, 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.
- Source Repository: https://github.com/5long/forwardable
- Feedback: https://github.com/5long/forwardable/issues
MIT license.