I refer to Python : Using the map function It says "map returns a specific type of generator in Python 3 that is not a list (but rather a 'map object', as you can see). " That is my understanding too. Generator object do not contain the values but is able to give you the values when you call it (next()). So my question is where are those values store ?
I tried the following experiment.
I would assume that when I delete the tuple, there would be no more values to do a next() but that's not the case. So my question is where are those values coming from ? Where are they stored after I delete the tuple ?
Code:
t1 = tuple(range(1000))
t2 = tuple(range(10000))
print(f'{t1[:10]} len = {len(t1):5d} size = {getsizeof(t1):5d}')
print(f'{t2[:10]} len = {len(t2):5d} size = {getsizeof(t2):5d}')
Output:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) len = 1000 size = 8040
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) len = 10000 size = 80040
Code:
m1 = map(lambda y: print(y), t1)
m2 = map(lambda y: print(y), t2)
print(f'size of m1 = {getsizeof(m1)}')
print(f'size of m2 = {getsizeof(m2)}')
Output:
size of m1 = 48
size of m2 = 48
Do the following a number of times:
next(m1)
next(m2)
Output:
23
23
Delete the tuple:
import gc
del t1
gc.collect()
t1
Output:
168
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-336-df561a5cc277> in <module>
2 del t1
3 gc.collect()
----> 4 t1
NameError: name 't1' is not defined
Continue next()
next(m1)
next(m2)
Output:
31
31
I'm still able to get values from map after deleting the tuple.
del
doesn't remove the tuple from memory, it just removes the variable.
The map
object has its own reference to the tuple -- it's a class instance variable variable.
Garbage collection doesn't remove a the tuple from memory until all references to it are destroyed. This will happen when the generator reaches the end (it should delete its own reference to avoid a memory leak) or if you delete the reference to the generator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.