从请求中获取布尔值



我正在从Falcon Api中的请求中获取布尔值。

样本URL:

localhost:8080/api/some-end-point/101?something=true

我想要

----------------------------------
| something     | Something_flag |
----------------------------------
| true          | True           |
----------------------------------
| false         | False          |
----------------------------------
| not provided  | True           |
----------------------------------

代码:

something_flag = req.get_param_as_bool('something') 
        if req.get_param_as_bool('something') else True

有没有更好的pythonic方法?

something_flag = req.get_param_as_bool('something') 
        if req.get_param('something') is not None else True

或者您可以使用参数默认

something_flag = req.get_param_as_bool('something', default=True)

对于1.2版,没有default,您可以使用required

try:
    something_flag = req.get_param_as_bool('something', required=True)
except HTTPBadRequest:
    something_flag = True

为什么不简单:

something_flag = req.get_param_as_bool('something') != False
True != False  # >>> True
False != False  # >>> False
None != False  # >>> True

相关内容

  • 没有找到相关文章

最新更新