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).
  • none singleton, 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 in statement) , as of python 3.2 sets (again, when used in).

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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

mysql - Pass value between different pages (form) with PHP -