I really am confused now. Is it an array, or is it not?
I really am confused now. Is it an array, or is it not?
I'm not aware of what the specification for a list would be, this is the closest I'm aware of: https://docs.python.org/3/reference/datamodel.html#objects-v..., 3.2 > Sequences > Mutable Sequences > Lists, "The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets"
CPython implementation is here: https://github.com/python/cpython/blob/e42b705188271da108de4...
If I'm talking about arrays with someone else, I would expect us to be thinking of arrays in the sense of C. Both C++ and Java use the same definition. Other languages that I'm aware of (e.g. Javascript, Go) have their own notions of arrays that you can't conflate with the C notion of an array.
From the C11 spec, §6.2.5.20: "An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type."
The heterogeneous use-case (that in C, etc. is usually a struct) is often filled by either tuples or (ordered) dictionaries in Python.
If you really want something a bit closer to a C array, you can fairly easily create a UserList that enforces the non-empty requirement and only allows items of a particular type.
https://docs.python.org/3/library/array.html
https://stackoverflow.com/questions/176011/python-list-vs-ar...
I'll probably rewrite that paragraph, since the main point is to tell people unfamiliar with Python that lists aren't actually linked lists.
And when the list needs to be resized and doubles itself, it's just doubling the space for object references?
Yes. Every Python object is held by the interpreter as a pointer to a PyObject struct on the (C) heap.
func = ctypes.cast(int(address), ctypes.py_object).value
Was kinda cool actually seeing that work.Yep, it is an array of pointers: https://github.com/python/cpython/blob/3.7/Include/listobjec...
> And when the list needs to be resized and doubles itself, it's just doubling the space for object references?
Basically yes. The growth pattern is not as aggressive as doubling though: https://github.com/python/cpython/blob/3.7/Objects/listobjec...
Thanks for sharing these links.
"The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ..."
What is the big-o amortized cost of using this pattern? Doubling has a easy proof that it's O(1) amortized.
To me it is kind of self-growing array that can hold objects of different types. The C++-ish equivalent would be something like std::vector<void*> / std::vector<MagicObjectBoxType> (there is no MagicObjectBoxType of course).
You compare this to, say, another fundamental data structure, the stack. In a stack you don't know how much memory each item takes up, so you can't just jump right to that item. Thus, the limitation is that you can only pop or push the last item on that stack. Once upon a time, stack-based languages were quite common.
A list, in contrast, is an ordered, polymorphic collection of objects which can have variable size. You can create resizable arrays by using a linked list. A resizable list would typically be implemented as an array with the last item being a pointer to the next array. I believe (but don't quote me on this), that Python uses links to increasingly large arrays so that there aren't too many links. That is, it dynamically increases how much memory to allocate to that array depending on how much data you're actually storing in that list.
A list is also polymorphic, meaning it can house multiple types of data. I sometimes hear a non-polymorphic, but resizable array called a "vector," but that's super confusing terminology. You can implement this by creating a resizable array of pointers, where each pointer points to a location in memory where the value, along with the data type, are stored. This way, all the pointers are the same size and you can jump right to that location by calculating it from the index, but you still get to have multiple value types.
The distinction is not universal, though. But in my humble opinion everyone who disagrees with me is wrong.