我有以下代码,我相信它看起来更像pythonic:
def info(self, msg, *args, **kwargs):
if 'extra' in kwargs:
kwargs['extra'].update({'session_id': self.session_id})
else:
kwargs['extra'] = {'session_id': self.session_id}
我正在尝试检查kwargs是否有一个名为extra的字典,如果确实有,则为其添加一个值,否则创建一个新的字典。
答案 0 :(得分:6)
可能更具可读性:
DELIMITER $$
CREATE OR REPLACE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `goods_input_total_amount-updateon-goods_input` BEFORE UPDATE
ON `gym`.`goods_input`
FOR EACH ROW BEGIN
DECLARE input_price INTEGER; /*use whatever datatype you have in your db for the price */
SELECT price_goods_input_price INTO input_price FROM goods_input_price
WHERE id_goods_input_price=NEW.id_goods_input_price LIMIT 1;
SET new.goods_input_total_amount=new.goods_input_quantity*input_price;
END$$
DELIMITER ;
答案 1 :(得分:3)
答案 2 :(得分:2)
你可以尝试使用除了:
try:
kwargs['extra'].update({'session_id': self.session_id})
except KeyError:
kwargs['extra'] = {'session_id': self.session_id}
请求宽恕比要求许可更容易。
答案 3 :(得分:1)
try:
kwargs['extra']['session_id'] = self.session_id
except KeyError:
kwargs['extra'] = {'session_id': self.session_id}