看到幾個字典生成式,記錄下來吧!
創(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':, 'PUT': , 'POST': }