【发布时间】:2021-08-19 20:59:15
【问题描述】:
下面的类函数为Account1 和Account2 创建一个对象。如何在 for 循环之外打印不同的对象值?
accounts= {'Account1': OrderedDict([('AccountName', 'Account1'), ('APIkey', 'xczccdfc'), ('APIsecret', 'sdasdadsadcd')]), 'Account2': OrderedDict([('AccountName', 'Account2'), ('APIkey', 'asdasdadcrf'), ('APIsecret', 'asdase346')])}
class Dummy:
pass
for account in accounts:
'''
Create a dummy function to update APIkey and APIsecret,
here then reference it as infos.key or infos.secret in stats.auth()
'''
dummy = Dummy()
dummy.key = accounts[account]["APIkey"]
dummy.secret = accounts[account]["APIsecret"]
print(Account1.key)
print(Account2.key)
【问题讨论】:
-
dummy.keys和dummy.secret会被覆盖吗? -
不——你看到我的回答了吗?它将在类
Dummy的每个实例下设置它们
标签: python-3.x class dictionary for-loop object