Python實現(xiàn)JavaBeans流程詳解
在JavaBeans中有這樣的一個描述:當一些信息需要使用類似于字典嵌套字典再嵌套列表這種很深的結(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
}
}]
而當我們要訪問比較深層結(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)文章
Python內(nèi)置的字符串處理函數(shù)詳細整理(覆蓋日常所用)
Python內(nèi)置的字符串處理函數(shù)整理,有字母處理、格式化相關(guān)、字符串搜索相關(guān)、字符串替換相關(guān)等等2014-08-08
Python使用Webargs實現(xiàn)簡化Web應用程序的參數(shù)處理
在開發(fā)Web應用程序時,參數(shù)處理是一個常見的任務,Python的Webargs模塊為我們提供了一種簡單而強大的方式來處理這些參數(shù),下面我們就來學習一下具體操作吧2024-02-02
Django原生sql也能使用Paginator分頁的示例代碼
這篇文章主要介紹了Django原生sql也能使用Paginator分頁的示例代碼,主要使用了count和__getslice__,有興趣的可以了解一下2017-11-11
Python辦公自動化之將任意文件轉(zhuǎn)為PDF格式
這種把某個文件轉(zhuǎn)為pdf枯燥無聊的工作,既沒有什么技術(shù)含量又累. 今天辰哥就教大家將任意文件批量轉(zhuǎn)為PDF,這里以日常辦公的word、excel、ppt為例,這三種格式的文件轉(zhuǎn)為PDF.需要的朋友可以參考下2021-06-06

