Steven D'Aprano wrote: > On Sun, 04 Jun 2017 02:15:33 +0000, Grant Edwards wrote: > >> On 2017-06-03, Thomas Jollans <t...@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. > > Actually you can, and you recognise that yourself: > > [...] >>> 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. > > Python strings would use a lot more memory if they were implemented in > the way the mystery book suggests they are (each character being > represented as a distinct object). > > In Python 3, for example: > > >>>> import sys >>>> sys.getsizeof("abcde") # actual memory consumption > 54 >>>> sum(sys.getsizeof(c) for c in "acbde") # theoretical > 250 > > > So we can tell the two implementations apart.
I str were implemented as a sequence of character objects it would still report >>> sys.getsizeof(tuple("abcde")) 88 The tuple doesn't store objects either, it holds pointers to objects and could easily be changed to hold the values of small integers, say. -- https://mail.python.org/mailman/listinfo/python-list