Understanding memory allocation for large integers in Python

后端 未结 2 1871
Happy的楠姐
Happy的楠姐 2020-12-29 02:38

How does Python allocate memory for large integers?

An int type has a size of 28 bytes and as I keep increasing the value of the int<

2条回答
  •  失恋的感觉
    2020-12-29 03:39

    It's actually easy. Python's int is not the kind of primitive you may be used to from other languages, but a full fledged object, with its methods and all the stuff. That is where the overhead comes from.

    Then, you have the payload itself, the integer that is being represented. And there is no limit for that, except your memory.

    The size of a Python's int is what it needs to represent the number plus a little overhead.

    If you want to read further, take a look at the relevant part of the documentation:

    Integers have unlimited precision

提交回复
热议问题