0 赞 0 踩
其他回答
不知道你问的是list最大长度还是存储的元素的最大字节数。
如果是list的最大长度,是有上限的。
如果是list的最大长度,是有上限的。
32位python的限制是 536870912 个元素64位python的限制是 1152921504606846975 个元素
热心网民 2小时前 0条评论 0 赞 0 踩
在 CPython里边 Python 的 list 是 PyObject* 的数组,如下:
ob_item是个数组,就是存值的地方。
有上限么?理论上来说有的…:
PS:据说 Jython 是ArrayList<PyObject>,没看过源码不确定 热心网民 2小时前 0条评论
typedef struct { PyObject_VAR_HEAD /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */ PyObject **ob_item; /* ob_item contains space for 'allocated' elements. The number * currently in use is ob_size. * Invariants: * 0 <= ob_size <= allocated * len(list) == ob_size * ob_item == NULL implies ob_size == allocated == 0 * list.sort() temporarily sets allocated to -1 to detect mutations. * * Items must normally not be NULL, except during construction when * the list is not yet visible outside the function that builds it. */ Py_ssize_t allocated;} PyListObject;
ob_item是个数组,就是存值的地方。
PyObject *PyList_GetItem(PyObject *op, Py_ssize_t i){ if (!PyList_Check(op)) { PyErr_BadInternalCall(); return NULL; } if (i < 0 || i >= Py_SIZE(op)) { if (indexerr == NULL) { indexerr = PyUnicode_FromString( "list index out of range"); if (indexerr == NULL) return NULL; } PyErr_SetObject(PyExc_IndexError, indexerr); return NULL; } return ((PyListObject *)op) -> ob_item[i];}
(最后一行)并且取元素的时候就是这么用的。有上限么?理论上来说有的…:
static intlist_resize(PyListObject *self, Py_ssize_t newsize){ //...前略 /* check for integer overflow */ if (new_allocated > PY_SIZE_MAX - newsize) { PyErr_NoMemory(); return -1; } else { new_allocated += newsize; } if (newsize == 0) new_allocated = 0; items = self->ob_item; if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *))) PyMem_RESIZE(items, PyObject *, new_allocated); else items = NULL; if (items == NULL) { PyErr_NoMemory(); return -1; } self->ob_item = items; Py_SIZE(self) = newsize; self->allocated = new_allocated; return 0;}
PS:据说 Jython 是ArrayList<PyObject>,没看过源码不确定 热心网民 2小时前 0条评论
0 赞 0 踩
登录后可回答 提交回答
关键词 :
- 上一个:作为一名商务英语的女生,于考研方向一无所知,会被点建议为?
- 下一个:都哪有不用学生证就能够进的大学自习室?
推荐资讯
点击排行