[英]How to fix inconsistent return statement in python?
我是 python 新手,我有這個項目我正在做一個帶有兩個函數的小項目,其中第一個返回第一次在字符串中發現差異的索引。 下一個函數執行此操作,但在字符串列表中。 現在,由於我是業余愛好者,我使用了過多的 if 和 else 語句,這導致返回語句過多,尤其是在第二個函數中,並且出現錯誤 [R1710:不一致的返回語句]。 我該如何修復它,誰能給我提供更好的代碼片段的清晰示例? 很抱歉問這么長的問題。
IDENTICAL = -1
def singleline_diff(line1, line2):
"""
Inputs:
line1 - first single line string
line2 - second single line string
Output:
Returns the index where the first difference between
line1 and line2 occurs.
Returns IDENTICAL if the two lines are the same.
"""
len1 = len(line1)
len2 = len(line2)
minimum_length = min(len1, len2)
if len1 != len2:
if minimum_length == 0:
return 0
for idx in range(minimum_length):
if line1[idx] == line2[idx]:
pass
else:
return idx
return idx + 1
for idx in range(len1):
if line1[idx] == line2[idx]:
pass
else:
return idx
return IDENTICAL
def multiline_diff(lines1, lines2):
"""
Inputs:
lines1 - list of single line strings
lines2 - list of single line strings
Output:
Returns a tuple containing the line number (starting from 0) and
the index in that line where the first difference between lines1
and lines2 occurs.
Returns (IDENTICAL, IDENTICAL) if the two lists are the same.
"""
line_no = singleline_diff(lines1, lines2)
len_lines1, len_lines2 = len(lines1), len(lines2)
if len_lines1 == len_lines2:
if (len_lines1 or len_lines2) == 0:
if len_lines1 == len_lines2:
return (IDENTICAL, IDENTICAL)
else:
idx = singleline_diff(lines1[line_no], lines2[line_no])
return (line_no, idx)
else:
idx = singleline_diff(lines1[line_no], lines2[line_no])
if line_no == IDENTICAL:
return (IDENTICAL, IDENTICAL)
elif line_no != IDENTICAL:
return (line_no, idx)
else:
return (line_no, 0)
看這里的代碼:
if len_lines1 == len_lines2:
return (IDENTICAL, IDENTICAL)
else:
idx = singleline_diff(lines1[line_no], lines2[line_no])
return (line_no, idx)
你可以把上面的東西寫成這樣:
if len_lines1 == len_lines2:
return (IDENTICAL, IDENTICAL)
idx = singleline_diff(lines1[line_no], lines2[line_no])
return (line_no, idx)
你只是不需要 else 塊來返回這個表達式,因為如果控件沒有進入 if 塊,這部分代碼將自動被調用。 希望能幫助到你。
OP 代碼中的語義錯誤在Abhishek Arya 的回答中
def your_function():
if not should_do():
return # NO RETURN VALUE!
# rest of the function
...是的,這將不再發出inconsistent-return-statements
;)
當您搜索inconsistent-return-statements
,這個 Q/A 也會彈出,我想為這些問題提供一個簡短的“常見問題”指南。
在某些情況下,有些函數(或“過程”,如果您想了解它的技術)只是做某事,但預計根本不會有任何返回值,
同時,可能會在函數開始時進行某種檢查,例如此函數運行是否有意義,您首先想到的是將整個函數代碼包裝在 if 語句中:
def your_function(article):
if display_content():
content = get_content(article)
# do some extensive logic to generate final content
# ...
print(content)
...這過於簡單化了,但讓我們希望您能想象一下,如果有更多的檢查和更多的代碼,這樣的編碼如何很快落入“意大利面條式代碼”+ 它還竊取了您的空間的一個“標簽”所以迫切需要適應您項目的最大行長。
幸運的是,與許多其他編程語言一樣,有一種方法可以通過在函數運行的任何地方return
ing 來提前結束函數,這意味着在任何“控制流”中——包括 if/elif/else、for/while循環,...
現在,您可能會快速return None
、 False
等。盡管它可以工作,但您仍然會收到 pylint inconsistent-return-statements
警告 - 了解為什么讓我們看看警告的消息:
函數中的所有 return 語句都應該返回表達式,或者它們都不應該返回。 pylint(不一致的返回語句)
從 pylint 的角度來看,如果您在return
之后放置任何內容,它將被視為一個expression 。 那么該怎么辦? 實際上,在 Python 中,您可以返回“nothing” (這又不是 Python 獨有的)
def your_function(article):
if display_content():
return
content = get_content(article)
# do some extensive logic to generate final content
# ...
print(content)
盡管在 Python 中返回“無”應該是(並且從技術上講,據我所知,它是)相當於return None
,但通過物理地編寫“ None
”,您表達了意圖,無論它是否隱含。
不要混淆這雖然與pylint(assignment-from-none)
( Assigning result of a function call, where the function returns None
) -其中兩個“ return
”與“ return None
”被認為是返回None
!
很常見的錯誤,尤其是在較大的代碼中,創建了一個導致根本不返回任何內容的代碼部分。 這不完全是OP 的情況,因為他們只使用了相同條件的否定,但pylint 不知道,所以這是它的思考過程:
if SOME_CONDITION: # ok, here's just another condition
return someReturnExpression # and ok, it returns SOMETHING, let's note that
elif OPPOSITE_OF_SOME_CONDITION: # ok, here's just another condition
return someReturnExpression # and ok, it returns SOMETHING, let's note that
# WAIT ! What?! THERE WAS NO "else:"! Hmmm...
# ...what happens if both conditions fail? NOTHING WOULD BE RETURNED!
# We need to make a warning about that!
# (fact that sometimes they return SOMETHING and sometimes NOTHING)
所以這個inconsistent-return-statements
可以解決
if SOME_CONDITION: # ok, here's some condition
return someReturnExpression # and ok, it returns SOMETHING, let's note that
else: # ok, here's else
return someReturnExpression # and ok, it returns SOMETHING, let's note that
# Ok, so if returns SOMETHING, else returns SOMETHING,
# so SOMETHING is returned every time! that's good!
...這本身有效,但它會產生另一個pylint問題
“返回”pylint(no-else-return)后不必要的“else”
參見 python 實際上鼓勵早期返回,因為它通常會導致代碼更清晰。
在函數運行期間return
ENDS(/exits) 函數和 pylint 看到 - 它看到如果條件為真,函數代碼將簡單地在那里結束 - 所以它, Abhishek Arya ,我和許多其他人建議只是繼續if 部分后的代碼:
if SOME_CONDITION:
return someReturnExpression
# ... some more code ...
# ... some more code ...
return someReturnExpression
只是不要將“just” return
與return SOMETHING
結合起來,
如果您確實需要返回 None,則在這種情況下只需顯式return None
def get_article(id):
article = find_article(id)
if article.id == 0:
return None
return article
這只是一個例子,這不是您真正檢查某些文章的方式;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.