过程返回值的使用¶
ID: py/procedure-return-value-used
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- maintainability
Query suites:
- python-security-and-quality.qls
Python 中的所有函数都返回值。如果函数没有 return
语句或没有一个 return
语句返回值,则该函数将返回 None
。但是,此值没有意义,应该忽略。
使用此类 “过程” 的返回值会让读者感到困惑,因为它暗示该值具有重要意义。
建议¶
不要使用过程的返回值;将 x = proc()
替换为 proc()
,并将对该值的任何使用替换为 None
。
示例¶
在此示例中,my_print
函数是一个过程,因为它不返回任何有意义的值。在后续代码中使用返回值具有误导性。
import sys
def my_print(*args):
print (args)
def main():
err = my_print(sys.argv)
if err:
sys.exit(err)
#FIXED VERSION
def main():
my_print(sys.argv)
#The rest of the code can be removed as None as always false
参考¶
Python 库参考:None.