我是裝飾師的新手,并且相信我在很大程度上了解他們的目的/他們的一般要點。不幸的是,我在使用此裝飾器及其在類中的使用時遇到了一些問題。我正在嘗試_profile_dict = {}使用創(chuàng)建的新密鑰填充字典profile + str(<counter value>)。我寧愿不實例化該類并將其設置為我if __name__ == '__main'語句中的變量,因此我嘗試使用該create_profiles函數(shù)執(zhí)行該類。這是我的代碼,_profile_dict = {}class Profile: def __init__(self): self.first_name = input('first name: ') self.last_name = input('last name: ') self.email = input('email: ') self.address1 = input('primary address: ') self.address2 = input('secondary address: ') self.city = input('city: ') self.country = input('country: ') self.province = input('province: ') self.zip = input('zip: ') self.phone = input('phone number:') self.cc_name = input('Name on credit card: ') self.cc_num = input('Credit card number: ') self.exp_month = input('card expiration month: ') self.exp_year = input('card expiration year: ') self.cvv = input('cvv: ')盡管從我這里讀過的內(nèi)容來看,類中的Python裝飾器,但我認為這是不可能的。是否有可能完全通過@classmethod完成我要達到的目標?任何幫助,將不勝感激。感謝您的時間。我覺得裝飾器以及我日程安排中的其他一些事項肯定會幫助我進入python的中間階段。
2 回答

jeck貓
TA貢獻1909條經(jīng)驗 獲得超7個贊
如果您不需要使用裝飾器,那么您可以測量字典的長度:
def _populate_profile_dict(self):
_profile_dict['profile' + str(len(_profile_dict))] = self.json_obj
print(_profile_dict)
添加回答
舉報
0/150
提交
取消