我想构建一个基本上是对象数组的类。我希望这个类返回其成员的属性列表。例如,来自班级:
class Animal():
def __init__(self, height, age):
self.height = height
self.age = age
dog = Animal(1,2)
cat = Animal(3,4)
我想创建一个类组:
my_group = Group([dog,cat])
print(my_group.height) #return [1,3] or (1,3) or array([1,3])
我想过这样做:
import inspect
def get_attribute(instance):
"""Return all attributes of an instance (except all internal built-in)"""
attributes = inspect.getmembers(instance, lambda a:not(inspect.isroutine(a)))
return [a for a in attributes if not(a[0].startswith('__') and a[0].endswith('__'))]
class Group():
def __init__(self, members):
self.members = members
attributes = [a[0] for a in get_attribute(list(members)[0])] #assuming members are all of the same class
for a in attributes:
setattr(self, a, [getattr(m, a) for m in self.members])
然后我可以将Group用于其他类,例如:
class Human():
def __init__(self, name, weight):
self.name = name
self.weight = weight
class Family(Group):
pass
alan = Human("alan", 1)
bob = Human("bob", 2)
my_family = Family([alan, bob])
print(my_family.weight) #returns [1, 2]
如果成员数量非常高,因为它循环遍历每个成员,但这似乎效率低下。基本上,我的代码可以工作,但我想使用map或类似的函数来加快它的速度。
答案 0 :(得分:1)
使用类属性:
class Animal():
all_data = {"height":[],"age":[]}
def __init__(self, height, age):
self.height = height
self.age = age
Animal.all_data["age"].append(age)
Animal.all_data["height"].append(height)
dog = Animal(1,2)
cat = Animal(3,4)
print(Animal.all_data["height"])
print(Animal.all_data["age"])
答案 1 :(得分:0)
也许您可以定义一种方法来保持动态:
class Animal():
def __init__(self, height, age):
self.height = height
self.age = age
dog = Animal(1,2)
cat = Animal(3,4)
class Group():
def __init__(self, members):
self.members = members
def get_all_attr(self, a):
return [getattr(m,a) for m in self.members]
my_group = Group([dog,cat])
print(my_group.get_all_attr("height")) #returns [1,3]
除非你使用延迟初始化,否则我不认为你不会在没有循环遍历成员的情况下逃脱。