Is An Element of a Sequence an Object?

Grant Edwards grant.b.edwards at gmail.com
Sat Jun 3 22:15:33 EDT 2017


On 2017-06-03, Thomas Jollans <tjol at tjol.eu> wrote:
> On 03/06/17 21:10, Jon Forrest wrote:
>
>> I'm learning about Python. A book I'm reading about it
>> says "... a string in Python is a sequence. A sequence is an ordered
>> collection of objects". This implies that each character in a string
>> is itself an object.

You can think about it that way if you want, and from observable
behavior you can't tell whether or not it's true.

>> This doesn't seem right to me, but since I'm just learning Python I
>> questioned the author about this. He gave an example the displays the
>> ids of string slices. These ids are all different, but I think that's
>> because the slicing operation creates objects.

That doesn't belie the assumption that inside strings, characters are
stored in memory as individual objects.  Strings are immutable, so
indexing and slicing can produce new objects or not -- it doesn't
matter.

>> 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,

Not in CPython, they don't.  In some other (hypothetical)
implementation, they could be.  The memory usage speed implications of
such a decision are not pleasant to contemplate.

-- 
Grant





More information about the Python-list mailing list