Python實現(xiàn)JavaBeans流程詳解
在JavaBeans中有這樣的一個描述:當(dāng)一些信息需要使用類似于字典嵌套字典再嵌套列表這種很深的結(jié)構(gòu)來儲存的時候,請改用類來儲存。實際上,這樣的思想也可以用于Python中。
場景
在Python中,以前可能會這樣寫嵌套字典結(jié)構(gòu)
school_list = [{ 'school_name': 'SZ', 'class_id': '001', 'stu_num': 45, 'student':{ 'stu_id': '001', 'stu_name': 'xiaohong', 'stu_score': 90 } }, { 'school_name': 'Fxxking U', 'class_id': '002', 'stu_num': 40, 'student':{ 'stu_id': '002', 'stu_name': 'xiaobai', 'stu_score': 98 } }]
而當(dāng)我們要訪問比較深層結(jié)構(gòu)中的數(shù)據(jù)時可能要這樣:
print(school_list[0]['student']['stu_id'])
這樣在取用時未免太麻煩,而且一旦嵌套結(jié)構(gòu)越深層,取用時就越麻煩。
JavaBeans in Python
如果借鑒JavaBeans的思維,將此用類實現(xiàn),會是以下這樣:
# School.py class School(object): def __init__(self,school_name='',class_id='',stu_num=0,student=None) -> None: self._school_name = school_name self._class_id = class_id self._stu_num = stu_num self._student = student @property def school_name(self): return self._school_name @school_name.setter def school_name(self,new_name): self._school_name = new_name @property def class_id(self): return self._class_id @class_id.setter def class_id(self,new_id): self._class_id = new_id @property def stu_num(self): return self._stu_num @stu_num.setter def stu_num(self,new_num): self._stu_num = new_num @property def student(self): return self._student @student.setter def student(self,new_student): self._student = new_student
# Student.py class Student(object): def __init__(self,stu_id='',stu_name='',stu_score=0) -> None: self._stu_id = stu_id self._stu_name = stu_name self._stu_score = stu_score @property def stu_id(self): return self._stu_id @stu_id.setter def stu_id(self,new_id): self._stu_id = new_id @property def stu_name(self): return self._stu_name @stu_name.setter def stu_name(self,new_name): self._stu_name = new_name @property def stu_score(self): return self._stu_score @stu_score.setter def stu_score(self,new_score): self._stu_score = new_score
我們將原有的嵌套字典數(shù)據(jù)轉(zhuǎn)換為兩個類實現(xiàn),且分別在School.py與Student.py兩個文件中,在類中我們對原本的數(shù)據(jù)以裝飾器粉飾為屬性從而使其可以進行讀取與修改。這樣一來,我們就可以用類屬性的方式去訪問我們想要的數(shù)據(jù)。
程序代碼:
from School import School from Student import Student student_007 = Student(stu_id='007',stu_name='零零漆',stu_score=99) school_Princeton = School(school_name='Princeton U',class_id='005',stu_num=1000,student=student_007) student_qnc = Student(stu_id='250',stu_name='千年蟲',stu_score=60) school_Fuxxking = School(school_name='Fuxxking U',class_id='009',stu_num=500,student=student_qnc) school_list = [school_Princeton,school_Fuxxking] for i in school_list: print(i.school_name) print(i.class_id) print(i.stu_num) stu = i.student print(stu.stu_name)
輸出結(jié)果:
Princeton U
005
1000
零零漆
Fuxxking U
009
500
千年蟲
總結(jié):將深層次的嵌套結(jié)果轉(zhuǎn)換為用類實現(xiàn)的好處是,在初始化類對象后,可以直接使用實例.屬性的方式訪問想要的數(shù)據(jù),且關(guān)鍵數(shù)據(jù)在類中定義的很詳細。
到此這篇關(guān)于Python實現(xiàn)JavaBeans流程詳解的文章就介紹到這了,更多相關(guān)Python JavaBeans內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyCharm利用pydevd-pycharm實現(xiàn)Python遠程調(diào)試的詳細過程
這篇文章主要介紹了PyCharm利用pydevd-pycharm實現(xiàn)Python遠程調(diào)試,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09Python?sklearn預(yù)測評估指標混淆矩陣計算示例詳解
這篇文章主要為大家介紹了Python?sklearn預(yù)測評估指標混淆矩陣計算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Python復(fù)制Excel表格中指定數(shù)據(jù)若干次的方法
本文介紹基于Python語言,讀取Excel表格文件數(shù)據(jù),并基于其中某一列數(shù)據(jù)的值,將這一數(shù)據(jù)處于指定范圍的那一行加以復(fù)制,并將所得結(jié)果保存為新的Excel表格文件的方法,需要的朋友可以參考下2024-02-02Python基于回溯法子集樹模板解決旅行商問題(TSP)實例
這篇文章主要介紹了Python基于回溯法子集樹模板解決旅行商問題(TSP),簡單描述了旅行商問題并結(jié)合實例形式分析了Python使用回溯法子集樹模板解決旅行商問題的相關(guān)實現(xiàn)步驟與操作技巧,需要的朋友可以參考下2017-09-09