Superdelegate
Delegate methods and properties to child objects in a terse, explicit style
Install / Use
/learn @bgschiller/SuperdelegateREADME
superdelegate
Delegate methods and properties to child objects in a terse, explicit style
Installation
$ pip install superdelegate
Consider the motivating example of a sorted list. In order to encapsulate the list and prevent clients from breaking the sorted order, we want to only allow certain methods through to the underlying list:
import bisect
class SortedList:
def __init__(self, *args, **kwargs):
self._lst = list(*args, **kwargs)
def insert(self, elem):
bisect.insort(self._lst, elem)
def __contains__(self, elem):
ix = bisect.bisect(self._lst, elem)
return ix != len(self._lst) and self._lst[ix] == elem
def __getitem__(self, key):
return self._lst.__getitem__(key)
def __reversed__(self):
return self._lst.__reversed__()
def __len__(self):
return self._lst.__len__()
def __iter__(self):
return self._lst.__iter__()
Much of that code is pretty repetitive. Can we do better? Yes! with superdelegate!
import bisect
from superdelegate import SuperDelegate, delegate_to
class SortedList(SuperDelegate):
def __init__(self, *args, **kwargs):
self._lst = list(*args, **kwargs)
def insert(self, elem):
bisect.insort(self._lst, elem)
def __contains__(self, elem):
ix = bisect.bisect(self._lst, elem)
return ix != len(self._lst) and self._lst[ix] == elem
__getitem__ = __reversed__ = __len__ = __iter__ = delegate_to('_lst')
FAQs
Does it work for properties? It does work for properties!
class OnlyGrowsOlder:
def __init__(self):
self._age
@property
def age(self):
return self._age
@age.setter
def age(self, new_age):
if new_age > self._age:
self._age = new_age
else:
raise ValueError('Time is like a boy band. It only flows in one direction')
class OlderWrapper(SuperDelegate):
def __init__(self):
self._ager
age = delegate_to('_ager')
Can you have multiple delgatees? You can!
class SandwichMaker:
def choose_cheese(self):
return 'swiss'
class NavyEngineer:
def lift_periscope(self):
return 'done'
class SubmarineTechnician(SuperDelegate):
def __init__(self):
self.sm = SandwichMaker()
self.ne = NavyEngineer()
choose_cheese = delegate_to('sm')
lift_periscope = delegate_to('ne')
Inspiration
This library was inspired by Ruby ActiveSupport's Module#delegate extension.
Related Skills
node-connect
352.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
