字典 复制思路

# 原始代码
room_sessid = req_local.get(UserElement.ROOM_SESSID)
recom_token = req_local.get(UserElement.RECOM_TOKEN)
   if room_sessid:
       info[UserElement.ROOM_SESSID] = room_sessid
   if recom_token:
       info[UserElement.RECOM_TOKEN] = recom_token

# 更改为
for key in [UserElement.ROOM_SESSID, UserElement.RECOM_TOKEN]:
   if key in req_local:
         info[key] = req_local.get(key)

# 变体 (源key 和目的key 不同)
for s_key, d_key in [
     (RoomMgrConst.ROOM_SESSID, UserElement.ROOM_SESSID),
     (RoomMgrConst.RECOM_TOKEN, UserElement.RECOM_TOKEN),
 ]:
     if s_key in props:
         update_info[d_key] = props[s_key]

你可能感兴趣的:(python)