Is An Element of a Sequence an Object?

Ben Finney ben+python at benfinney.id.au
Sat Jun 3 20:03:11 EDT 2017


Jon Forrest <nobozo at gmail.com> writes:

> I'm learning about Python. A book I'm reading about it

Can you say which book, and where in the book it says this?

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

It does imply that.

To phrase it that way might be slightly misleading, though. It is *not*
true, for example, to say that the characters in the string already
exist as objects.

I would prefer to state the implication this way:

*When* treating the string as a sequence, it *produces* each character
as a separate (length 1) string object.

That is, the one-character string objects are generated when needed, and
do not exist otherwise.

> 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's a separate matter :-) A slice is itself an object, which allows
dealing with part of a sequence.

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

The term “object” is correct in everything you've presented so far; I
think that term is fine for what you're saying.

I think, rather, that you may have an incorrect understanding of a
sequence. The objects produced by an operation on a sequence – a slice,
or iterating the sequence – do not necessarily exist as distinct objects
before that operation.

> Am I on the right track here?

I hope that helps.

-- 
 \                “The old believe everything; the middle-aged suspect |
  `\    everything; the young know everything.” —Oscar Wilde, _Phrases |
_o__)                 and Philosophies for the Use of the Young_, 1894 |
Ben Finney




More information about the Python-list mailing list