【发布时间】:2022-01-21 20:57:47
【问题描述】:
有可能做这样的事情吗?
class child:
def __init__(self):
self.figure = "Square"
self.color = "Green"
bot = child()
bot_parameters = ['color', 'figure']
[print(bot.i) for i in bot_parameters] #Attribute Error from the print function.
我知道我可以使用__dict__ 访问参数值,但我想知道是否可以连接参数以编程/动态方式获取值。
【问题讨论】:
-
您也可以按照question 中的建议使用
getattr() -
目前还不清楚您期望达到的目标。确实没有定义的
size的值应该是多少? 连接参数以编程方式/动态获取值 不清楚您的意思是什么 -
例如我可以编写代码: print(bot.figure) print(bot.color) 但我的问题是它是否可以做类似的事情:for parameter in ['figure', 'color ']: 打印(bot.parameter)
标签: python oop dynamic properties