Is An Element of a Sequence an Object?
Steve D'Aprano
steve+python at pearwood.info
Sun Jun 4 05:51:37 EDT 2017
On Sun, 4 Jun 2017 04:50 pm, Peter Otten wrote:
> Steven D'Aprano wrote:
>> 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.
Perhaps; but calling getsizeof directly is not enough to report the full memory
usage of a collection. You need something like Raymond Hettinger's recursive
getsizeof:
code.activestate.com/recipes/577504/
Using that in Python 3.5 I get 174 bytes, and in 3.2 I get 204 bytes.
The specific values will differ according to the platform, the version, and the
implementation, but the conclusion should still holds.
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list