您好,登錄后才能下訂單哦!
看到幾個字典生成式,記錄下來吧!
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)
結果:
{'GET': <function Test.read at 0x010C0858>, 'PUT': <function Test.update at 0x010C08E8>, 'POST': <function Test.create at 0x010C08A0>}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。