最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)JavaBeans流程詳解

 更新時間:2023年01月14日 16:45:06   作者:牛油菠蘿包  
這篇文章主要介紹了Python實現(xiàn)JavaBeans流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

在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)文章

最新評論

盐池县| 囊谦县| 社会| 乳山市| 陇西县| 汪清县| 杨浦区| 高青县| 清镇市| 恩平市| 康马县| 全州县| 绥江县| 临漳县| 轮台县| 城步| 娱乐| 平武县| 石城县| 辽中县| 富平县| 凯里市| 大方县| 黄平县| 诸暨市| 楚雄市| 黄浦区| 内丘县| 简阳市| 新建县| 道真| 桃源县| 宿松县| 绥德县| 京山县| 齐齐哈尔市| 三河市| 湛江市| 裕民县| 长春市| 玛多县|