Unnecessary delete statement in function¶
ID: py/unnecessary-deleteKind: problemSecurity severity: Severity: warningPrecision: highTags: - maintainability - useless-codeQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
Passing a local variable to adel statement results in that variable being removed from the local namespace. When exiting a function all local variables are deleted, so it is unnecessary to explicitly delete variables in such cases.
Recommendation¶
Remove thedel statement.
Example¶
In the function below, the variablex is assigned a value that is used for a calculation, and is then explicitly deleted before the function exits. In this case, the delete statement can be removed without changing the behavior of the function.
defunnecessary_delete():x=get_some_object()do_calculation(x)delx# This del statement is unnecessary
References¶
Python:The ‘del’ statement.
Python/C API Reference Manual:Reference counts.