python字典生成式

看到幾個(gè)字典生成式,記錄下來吧!

創(chuàng)新互聯(lián)主營日喀則網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā),日喀則h5小程序制作搭建,日喀則網(wǎng)站營銷推廣歡迎日喀則等地區(qū)企業(yè)咨詢

callmap = {'GET': 'read', 'POST': 'create',
            'PUT': 'update', 'DELETE': 'delete'}

# def __new__(metaclass, name, bases, namespace):
class Test(object):

    def read(self):
        print('get')
        return 'get'

    def create(self):

        print('post')
        return 'post'

    def update(self):
        print('put')
        return 'put'
    
cls = Test
# 遍歷callmap字典,尋找cls類中method方法,如果有則返回鍵和值;如果沒有,忽略
crud = {
    (http_method,None): getattr(cls, method)
    for http_method, method in list(callmap.items())
    if getattr(cls, method, None) is not None
    }
print(crud)

結(jié)果:

{'GET': <function Test.read at 0x010C0858>, 
'PUT': <function Test.update at 0x010C08E8>, 
'POST': <function Test.create at 0x010C08A0>}

本文標(biāo)題:python字典生成式
文章出自:http://bm7419.com/article0/gipjio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、電子商務(wù)、網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)