Skip to content

Instantly share code, notes, and snippets.

@sebastianw
Created August 31, 2018 12:06
Show Gist options
  • Save sebastianw/43341df6c7c4deaa189e26da01a1c0ca to your computer and use it in GitHub Desktop.
Save sebastianw/43341df6c7c4deaa189e26da01a1c0ca to your computer and use it in GitHub Desktop.
A python object which forwards all get/set requests (and functions) to a list of (identical) child objects
class ObjectList(object):
def __init__(self, child_objects):
self.child_objects = child_objects
def __getattr__(self, name):
def looper(*args, **kwargs):
for obj in self.child_objects:
attr = getattr(obj, name)
attr(*args, **kwargs)
return looper
def __setattr__(self, key, value):
if key in ['child_objects']:
super(ObjectList,self).__setattr__(key,value)
else:
for obj in self.child_objects:
setattr(obj, key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment