70 lines
1.4 KiB
Plaintext
70 lines
1.4 KiB
Plaintext
This rule raises an issue when `return` or `yield` are used outside of a function.
|
|
|
|
== Why is this an issue?
|
|
|
|
`yield` and `return` only make sense in the context of functions. Using them outside a function raises a `SyntaxError`.
|
|
|
|
If the goal is to break out of a loop, use `break` instead of `return`.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,python,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
a = 1
|
|
while a < 3:
|
|
if a % 2 == 0:
|
|
return # Noncompliant: return is outside of a function
|
|
a += 1
|
|
|
|
for n in range(5):
|
|
yield n # Noncompliant: yield is outside of a function
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
|
----
|
|
a = 1
|
|
while a < 3:
|
|
if a % 2 == 0:
|
|
break
|
|
a += 1
|
|
|
|
def gen():
|
|
for n in range(5):
|
|
yield n
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Python Documentation - https://docs.python.org/3/reference/expressions.html?highlight=yield#yield-expressions[Yield expressions]
|
|
* Python Documentation - https://docs.python.org/3/reference/simple_stmts.html?highlight=return%20tatement#the-return-statement[Return statement]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this use of "xxx".
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 17 Mar 2015, 07:59:40 Elena Vilchik wrote:
|
|
Pylint rules
|
|
|
|
E0104: Return outside function
|
|
|
|
E0105: Yield outside function
|
|
|
|
endif::env-github,rspecator-view[]
|