【问题标题】:What does the output of the Python extend function mean? [duplicate]Python 扩展函数的输出是什么意思? [复制]
【发布时间】:2019-10-18 19:26:22
【问题描述】:

extend 函数的返回值 (None) 表示什么。

common_birds = ["chicken", "blue jay", "crow", "pigeon"]
birds_seen = ["sparrow", "green finch", "gold finch"]

print (common_birds.extend(birds_seen))
# returns None

common_birds.extend(birds_seen)
print (common_birds)
# returns the extended list

【问题讨论】:

  • 这意味着该函数没有返回任何内容,因为它修改了用于调用它的列表
  • 你可能想print(common_birds + birds_seen)

标签: python


【解决方案1】:

这意味着该方法不会返回任何内容,而是通过扩展列表“就地”工作。您不会返回新列表,而是更改旧列表。

【讨论】:

    【解决方案2】:

    这意味着该方法没有您可以使用的结果。相反,它有副作用。

    希望唯一的副作用是您所期望的,即列表被扩展。

    在您的生活中,您会发现 void 方法有多种副作用,其中一些是不需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 2011-07-08
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多