python语法-None与空字符串‘‘

背景:前端传过来username进行修改,可假如username为空字符串,却不能修改username,出现了逻辑错误。

假如用户名字为张三,传username=‘张三1’,username='张三2'都能修改。错误为传username=''不能覆盖。

原来代码中修改的逻辑为:

    if username:
        r_user.username = username

修改策略可将代码变为:(假如前端决定不修改username,即username不传的情况,后端是一直将username的默认值设为None)

    if username is not None:
        r_user.username = username

 在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False 

参考:进一步解决策略

if XXX is not None:

python语法-None与空字符串‘‘_第1张图片

你可能感兴趣的:(python,java,数据库,开发语言)