On Sun, 04 Jun 2017 02:15:33 +0000, Grant Edwards wrote:
> On 2017-06-03, Thomas Jollans <[email protected]> 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.
--
Steve
--
https://mail.python.org/mailman/listinfo/python-list