列表接受可迭代对象作为输入。但是,当输入是 int 或 float 时,例如列表(1),这是不被接受的。为什么?

Lists accept iterable objects as inputs. However, when the input is an int or float, e.g. list(1), this is not accepted. Why?

我在做有关继承和 class 对象的 Python 练习时尝试使用列表属性。我意识到 list([1,2,3]) 是有效的,因为列表本身是可迭代的,但像 list(1) 这样的东西会 return 一个错误。单个对象本身不是可迭代的吗?但是,像 list("this is a list") 这样的具有多个字符的字符串不会 return 错误,这进一步增加了我的困惑(当然,字符串是单个对象)。为什么会这样?

from  cpython/listobject.c (starting line 2675)
/*[clinic input]
list.__init__
    iterable: object(c_default="NULL") = ()
    /
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
[clinic start generated code]*/

我查看了 https://github.com/python/cpython/blob/master/Objects/listobject.c 列表 class 的源代码,似乎第 2675-2721 行可能有我正在寻找的答案,但作为新手,我需要有人向我解释创建列表的过程。

list() 函数只接受迭代。 Iterables 是可以迭代的对象。程序无法遍历整数,但 可以 遍历 single-character 字符串。