I was just checking the size of some datatypes in Python 3 and I observed this.
import sys
val = None
print(sys.getsizeof(val))
The output was 16 as expected.
I tried making a list of 1000 locations of None and I expected the size to be 16*1000 = 16000 or more. But the result I got was different.
import sys
val = [None]*1000
print(sys.getsizeof(val))
The output was 8064. Nearly the half of the size I expected.
What is the reason for this.? Why memory allocated is less.?
import sys
val = None
print(sys.getsizeof(val))
Answer:
16
val = []
print(sys.getsizeof(val))
Answer:
72
val = [None]
print(sys.getsizeof(val))
Answer:
80
so [None]*1000 = 1000* 8 + 72 = 8072
Note: No of bytes may vary depending on the environment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With