python - CPython - Print out all immutable objects -
as may have noticed before, cpython stores single copy of identical immutable objects.
e.g.
>>> = "hello" >>> b = "hello" >>> b true >>> a, b = 7734, 7734 >>> b true it appears hashing assume heap performed after type inferencing
>>> a, b = 7734, 07734 >>> b false >>> a, b = 7734, 017066 >>> b true is there way introspect interpreter , print out supposed heap of immutable objects?
no, interned objects maintained in range of locations, no 1 method exists list them all.
- strings can interned, discovered, , can intern strings using
intern()function. - small integers between -5 , 256 interned.
- tuples reused; empty tuple (
()) singleton, , 2000 each of tuple sizes 1 through 20 kept cached recycling. (just tuple objects, not contents). nonesingleton,ellipsis,notimplemented,true,false.- as of python 3.3, instance
__dict__dictionaries can share keys save on memory. - the compiler can mark immutable (and in circumstances, mutable) sourcecode literals constants, store them such bytecode , re-use them each time bytecode run. applies strings, numbers, tuples, lists (if used
instatement) , as of python 3.2 sets (again, when usedin).
there may more haven't discovered yet.
these optimizations avoid heap churn. , apart none, ellipsis, notimplemented, true , false being singletons cpython-specific optimisations, not part of python language definition itself.
Comments
Post a Comment