我在对继承和类对象进行python练习时正在尝试列表属性。我意识到list([1,2,3])
是有效的,因为列表本身是一种疑问,但是list(1)
之类的东西会返回错误。单个对象本身不是一个值得一提的吗?但是,一个带有多个字符的字符串(例如列表("这就是列表"((不会返回错误,进一步增加了我的困惑(被授予,字符串是一个对象(。为什么会这样?
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/master/objects/listobject.c上查看了列表类的源代码,似乎行2675-2721可能有答案i'我寻找但作为新手,我需要有人向我解释创建列表的过程。
list()
功能仅接受迭代。迭代是可以迭代的对象。程序无法在整数上迭代,但是它可以通过单字符串进行迭代。