Python實(shí)現(xiàn)JavaBeans流程詳解
在JavaBeans中有這樣的一個(gè)描述:當(dāng)一些信息需要使用類似于字典嵌套字典再嵌套列表這種很深的結(jié)構(gòu)來(lái)儲(chǔ)存的時(shí)候,請(qǐng)改用類來(lái)儲(chǔ)存。實(shí)際上,這樣的思想也可以用于Python中。
場(chǎng)景
在Python中,以前可能會(huì)這樣寫(xiě)嵌套字典結(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)我們要訪問(wèn)比較深層結(jié)構(gòu)中的數(shù)據(jù)時(shí)可能要這樣:
print(school_list[0]['student']['stu_id'])
這樣在取用時(shí)未免太麻煩,而且一旦嵌套結(jié)構(gòu)越深層,取用時(shí)就越麻煩。
JavaBeans in Python
如果借鑒JavaBeans的思維,將此用類實(shí)現(xiàn),會(huì)是以下這樣:
# 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)換為兩個(gè)類實(shí)現(xiàn),且分別在School.py與Student.py兩個(gè)文件中,在類中我們對(duì)原本的數(shù)據(jù)以裝飾器粉飾為屬性從而使其可以進(jìn)行讀取與修改。這樣一來(lái),我們就可以用類屬性的方式去訪問(wèn)我們想要的數(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='千年蟲(chóng)',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
千年蟲(chóng)
總結(jié):將深層次的嵌套結(jié)果轉(zhuǎn)換為用類實(shí)現(xiàn)的好處是,在初始化類對(duì)象后,可以直接使用實(shí)例.屬性的方式訪問(wèn)想要的數(shù)據(jù),且關(guān)鍵數(shù)據(jù)在類中定義的很詳細(xì)。
到此這篇關(guān)于Python實(shí)現(xiàn)JavaBeans流程詳解的文章就介紹到這了,更多相關(guān)Python JavaBeans內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyCharm利用pydevd-pycharm實(shí)現(xiàn)Python遠(yuǎn)程調(diào)試的詳細(xì)過(guò)程
這篇文章主要介紹了PyCharm利用pydevd-pycharm實(shí)現(xiàn)Python遠(yuǎn)程調(diào)試,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09Python?sklearn預(yù)測(cè)評(píng)估指標(biāo)混淆矩陣計(jì)算示例詳解
這篇文章主要為大家介紹了Python?sklearn預(yù)測(cè)評(píng)估指標(biāo)混淆矩陣計(jì)算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Python復(fù)制Excel表格中指定數(shù)據(jù)若干次的方法
本文介紹基于Python語(yǔ)言,讀取Excel表格文件數(shù)據(jù),并基于其中某一列數(shù)據(jù)的值,將這一數(shù)據(jù)處于指定范圍的那一行加以復(fù)制,并將所得結(jié)果保存為新的Excel表格文件的方法,需要的朋友可以參考下2024-02-02Python基于回溯法子集樹(shù)模板解決旅行商問(wèn)題(TSP)實(shí)例
這篇文章主要介紹了Python基于回溯法子集樹(shù)模板解決旅行商問(wèn)題(TSP),簡(jiǎn)單描述了旅行商問(wèn)題并結(jié)合實(shí)例形式分析了Python使用回溯法子集樹(shù)模板解決旅行商問(wèn)題的相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下2017-09-09