【发布时间】:2019-01-26 06:37:52
【问题描述】:
是否有充分的理由将对象列表存储为“子属性”?在下面的示例中,我将几个动物对象存储在 animals 属性下的 Zoo 中,例如 zoo.animals.<animal object referenced by name>。这种语法可以更轻松地访问存储的动物的属性,我想知道这种结构是否存在我尚未考虑的缺点:
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
【问题讨论】:
-
这实际上是我工作场所标准的一部分,我无法确定这是好的还是坏的做法,但我可以说的是,如果您打算添加更多“子- attributes" ,例如,您有一种特定类型的鸟,并且您想要存储它的爪子大小,这将在 num_legs 下抽象出来并在其上扩展。所以你会有
zoo.animals.bird.num_legs.talon_size,这可能会变得很混乱,我确实看到很多,但是如果你不希望很快添加它,我从来没有看到它有什么问题。 -
这只是不必要的,并没有多大帮助
标签: python oop attributes coding-style