Skip to content

Instantly share code, notes, and snippets.

@ramnes
Last active August 29, 2015 14:04
Show Gist options
  • Save ramnes/f61ac69dbc71478f81ff to your computer and use it in GitHub Desktop.
Save ramnes/f61ac69dbc71478f81ff to your computer and use it in GitHub Desktop.
class ObjectList(list):
"""A list that can call its objects methods/variables and returns their
own returns in a list. Obviously, the list should contains only one type of
objects.
Example of use:
>>> foo = ObjectList(["bar", "baZ"])
>>> foo.islower()
[True, False]
>>> foo.append(42)
>>> foo.islower()
Traceback (most recent call last):
...
AttributeError: 'int' object has no attribute 'islower'
>>> foo
['bar', 'baZ', 42]
"""
def __getattr__(self, name):
attrlist = ObjectList()
for o in self:
if o is not None:
attr = getattr(o, name)
attrlist.append(attr)
return attrlist
def __call__(self, *args, **kwargs):
retlist = []
for o in self:
if callable(o):
o = o(*args, **kwargs)
retlist.append(o)
return retlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment