0
我想弄清楚如何在字典中存儲類Job
的實例。我想達到這樣的{location : "London" , fee:500,fee:1200,location:"London" }
。我通過將條目保存在類變量中嘗試了下面的解決方案,但我只能獲取該值。我無法定義密鑰。在Python中將類的實例存儲在字典中
class Job:
instances = dict()
rate = 1.04
def __init__(self, location, salary, description, fee) :
self.location = location
self.salary = salary
self.description = description
self.fee = fee
self.instances[self.location] = location
self.instances[self.fee] = fee
def Charge(self):
self.fee = int(self.fee + Job.rate)
Job1 = Job("london",23000,"Accounts Assistant",1200)
Job2 = Job("london",25000,"Accounts Assistant",500)
print(Job.instances)
輸出:
{1200: 1200, 'london': 'london', 500: 500}
非常感謝你的建議這個作品 –