为良好的编码实践,“功能和方法结束时无用的回报”是什么意思? 我使用spyder创建了一个网络刮板,到目前为止,事情的移动顺利。作为新秀,我发现Spyder的代码分析功能对于改善代码的标准很有用。 Howev ...

问题描述 投票:0回答:1
代码运行良好,并执行应有的一切。但是,我并不真正了解的是Spyder的陈述,即代码块中有一个无用的调用。 这是它专门说的:

,但是从我收集的内容来看,每个功能调用都是在此编码块中所必需的。我会错过什么?
    

Python函数默认情况下隐式返回enter image description herereturn。以下功能定义等效。

None

python function spyder conventions
1个回答
4
投票

完全没有语句 - 这表明您在调用函数时不应将名称分配给返回值,OR

阐明

def foo(): pass def foo(): return def foo(): return None
,指示可以返回有意义值的函数的“无结果”,
仅使用

return

制作一个不返回没有有意义的值停止执行的函数。
  1. 示例情况1:
    return None
  2. 该函数隐含地返回
    return
    ,您是不应该发行的
  3. def switch_first_last_in_place(lst): 'switch the first and last elements of a list in-place' lst[0], lst[-1] = lst[-1], lst[0]
    示例情况2:
None

该功能明确返回
result = switch_first_last_in_place([1, 2, 3])
未找到用户。作业喜欢

def get_user_info_from_database(username):
    'fetches user info, returns None if user does not exist'
    if user_exist(username):
        return query_db(username)
    else:
        return None
期望。部分 None

不必要的
,但我喜欢在
result = get_user_info_from_database('Bob')

是有意义的回报值的情况下明确说明。

示例3:
else:
    return None

None

仅用于脱离功能。
我不喜欢示例函数末尾的裸露。它不用于结束执行,这些功能无法返回其他值。我会删除它。
    

您误会了。警告不是谈论您正在调用的任何功能。它指的是您对
def assert_exists(thing, *containers):
    'raise AssertionError if thing cannot be found in any container'
     for container in containers:
         if thing in container:
             return
     raise AssertionError
关键字的使用。

这个功能: return

等于此功能:

return

等于此功能:

return

警告说,您的

def print_hello(): print("Hello") return
声明是没有用的,不需要的。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.