是否有充分的理由将对象列表存储为“子属性”?在下面的示例中,我在animals
属性(例如例如 zoo.animals.<animal object referenced by name>
)下的Zoo中存储了几个动物对象。这种语法使访问存储的动物的属性更加容易,我想知道是否还没有考虑到这种构造的缺点:
class Animal(object):
def __init__(self, name, num_legs, furry):
self.name = name
self.num_legs = num_legs
self.furry = furry
class ObjAsAttributes(object):
def __init__(self, **kwargs):
for k,v in kwargs.items():
setattr(self, k, v)
class Zoo(object):
def __init__(self, animals):
self.name = 'my zoo'
self.hours = '8am-6pm'
animals = {animal.name:animal for animal in animals}
self.animals = ObjAsAttributes(**animals)
animal_list = [Animal(name='bird', num_legs=2, furry=False),
Animal(name='giraffe', num_legs=4, furry=True),
Animal(name='octopus', num_legs=8, furry=False)]
zoo = Zoo(animal_list)
zoo.animals.bird.num_legs
# returns 2
答案 0 :(得分:2)
我认为,这样做会使您的代码难以调试,不灵活且不可读。这是个坏主意。例如,会发生什么情况:
for name in vars(obj):
print(getattr(obj, name))
属性旨在“保留”建模对象的数据或“描述特征”。您仅应将它们用于此目的。
主要是在寻求快速便捷的访问权限时,请使用dict
或OrderedDict
:
class Animal(object):
def __init__(self, name, num_legs, furry):
self.name = name
self.num_legs = num_legs
self.furry = furry
class Zoo(object):
def __init__(self, animals):
self.name = 'my zoo'
self.hours = '8am-6pm'
self.animals = {animal.name:animal for animal in animals}
animal_list = [Animal(name='bird', num_legs=2, furry=False),
Animal(name='giraffe', num_legs=4, furry=True),
Animal(name='octopus', num_legs=8, furry=False)]
zoo = Zoo(animal_list)
zoo.animals['bird'].num_legs
# returns 2