Last active
August 29, 2015 14:04
-
-
Save ramnes/f61ac69dbc71478f81ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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