Is An Element of a Sequence an Object?

Chris Angelico rosuav at gmail.com
Sat Jun 3 19:58:23 EDT 2017


On Sun, Jun 4, 2017 at 9:42 AM, Jon Forrest <nobozo at gmail.com> wrote:
> On 6/3/2017 12:38 PM, Thomas Jollans wrote:
>
>>> I'd like to suggest an explanation of what a sequence is
>>> that doesn't use the word 'object' because an object has
>>> a specific meaning in Python.
>>>
>>> Am I on the right track here?
>>
>> No, strings don't internally store the characters as objects, and yes,
>> the slicing operation creates objects. However, strings *are* sequences,
>> sequences *are* ordered collections of objects.
>
> I don't see how both can be true. "Object" has a clear meaning in
> Python, and the contents of a sequence don't meet the requirements,
> as I understand them.
>
> If there were some way of measuring the number of objects in
> a program, then it would be easy to prove (or disprove) my hypothesis.
> In other words this program
>
> a = "abc"
>
> would have the same number of objects as this program
>
> a = "abcdefghijklmnopqrstuvwzyz"

A sequence doesn't necessarily "contain" anything. As has been
mentioned, a range object is a sequence, but it creates integer
objects lazily. The point of a sequence is that you can do this:

for thing in sequence:

and whatever is in 'thing' must, by definition, be an object. Since
you can do this with a string, a string is, indeed, a sequence.
(Technically there's more to a sequence than just being iterable, but
this is the part that matters here.) The semantics of Python would be
the same if all those objects were indeed pre-created, but for memory
efficiency, they aren't.

ChrisA



More information about the Python-list mailing list