以下是一个使用属性条件初始化一组豆子的代码示例:
class Bean:
def __init__(self, color, size):
self.color = color
self.size = size
def __str__(self):
return f"Bean(color={self.color}, size={self.size})"
def __repr__(self):
return self.__str__()
def initialize_beans():
beans = []
beans.append(Bean(color="red", size="small"))
beans.append(Bean(color="green", size="medium"))
beans.append(Bean(color="blue", size="large"))
return beans
beans = initialize_beans()
for bean in beans:
print(bean)
这里我们定义了一个Bean
类,它具有颜色和大小两个属性。在initialize_beans
函数中,我们按照属性条件初始化了一组豆子,并将它们添加到一个列表中。最后,我们遍历列表并打印每个豆子的属性。
输出结果为:
Bean(color=red, size=small)
Bean(color=green, size=medium)
Bean(color=blue, size=large)
你可以根据自己的需求修改属性的名称和类型。