如何从函数中返回布尔值的最佳实践:
- 硬编码
- 作为变量
代码 1:
(status, response) = self.generate_pdf(html_data)
if not status:
return (False, response)
代码 2:
(status, response) = self.generate_pdf(html_data)
if not status:
return (status, response)
我认为返回硬编码值(False
或True
)是好的。
[更多细节]
我总是从每个函数返回两个参数:status
和response
。一个status
是布尔对象,即其值为True
或False
。一个response
是字典对象。
因为status
有一个False
的值,即status
是指向False
的地址。
>>> id(False)
493227104
>>> status = False
>>> id(status')
493227104
那么返回引用变量名称是良好的实践还是实际的布尔值?