4 ms·
Newbie Python question: What does a Python List do that a Ruby Array doesn't do? Regardless of the implementation, this is a question of the abstract behaviour
by seanhandley 13y ago
Newbie Python question: What does a Python List do that a Ruby Array doesn't do? Regardless of the implementation, this is a question of the abstract behaviour of a data structure. If a Python List is functionally equivalent to an Array (which is a sequence) then why coin it as a "List" ?
- Macha 13y agoRuby's Array, Python's list, C++'s vector and Java's ArrayList are all the same thing: a variable length collection with an implementation backed by an array that's replaced with a larger array and the information copied over for you if you use all the space. Fundamentally they consist of a primitive array, and a variable containing the length of the collection. Personally I would usually call the resulting class a list, but that's because most of my programming experience is from Python and Java. I will give that C++'s name is the most explicit (especially since C++ also has std::list which is a linked list). In C and similar languages however, array refers to something different. In those cases an array is a fixed size collection, i.e. at creation time you need to tell it that your array is 10 items long, and if you need to add an eleventh item, you need to create a new larger array and copy it over in your own code. In short, Ruby's Array is not functionally equivalent to an array, because it handles a lot more than primitive arrays can. You can't just append to an array, or figure out how many items are in an array without separate knowledge of its size. To add to the confusion, a python 'array' is a data type which is effectively a fixed type version of a python list. It's still dynamically sized, but it includes the type information once for the entire array rather than per item to make things a bit more efficient.
- seanhandley 13y agoThanks for the explanation. As ever, it's not the name of the structure that's wholly important in determining its purpose; but the interface to which it adheres.