As an example, I have the following Python code:
>>> x = 9.89
Now I know the type will be determined as float dynamically during runtime, but I am unsure how memory is allocated. Would memory for the size of a float be allocated dynamically after the type is determined? Is the amount of memory allocated always the same?
解决方案Python does a lot of allocations and deallocations. All objects,
including "simple" types like integers and floats, are stored on the
heap. Calling malloc and free for each variable would be very slow.
Hence, the Python interpreter uses a variety of optimized memory
allocation schemes. The most important one is a malloc implementation
called pymalloc, designed specifically to handle large numbers of
small allocations. Any object that is smaller than 256 bytes uses this
allocator, while anything larger uses the system's malloc.
Memory allocation works at several levels in Python. There’s the
system’s own allocator, which is what shows up when you check the
memory use using the Windows Task Manager or ps. Then there’s the C
runtime’s memory allocator (malloc), which gets memory from the system
allocator, and hands it out in smaller chunks to the application.
Finally, there’s Python’s own object allocator, which is used for
objects up to 256 bytes. This allocator grabs large chunks of memory
from the C allocator, and chops them up in smaller pieces using an
algorithm carefully tuned for Python.
and specifically for floats:
floats also use an immortal & unbounded free list.
So no, it would only be allocated if there are no more free float spots in pythons free list, which depends on previous float usage in your program.
Ultimately, python is doing the memory management for you, so even a solid answer to your question won't give you much insight.
Additional discussion can be found at Python: garbage collection fails?