Python中的多个属性
问题描述:
class Dog(object):
species = "mammal"
def __init__(self, breed, name, color):
self.breed = breed
self.name = name
sam = Dog(breed="lab", name="maorian", color="white")
但是,我无法使用“颜色”属性。为什么?Python中的多个属性
答
您没有在初始化程序中设置它。在self.name = name
行后加self.color = color
。
谢谢。但是如果我有100个属性,这是不是太乱了? –
@Nirupbelongokar如果你有一百个属性,那是一种代码味道:https://sourcemaking.com/refactoring/smells/large-class – jonrsharpe