Nel primo caso consumi un iteratore già esausto, per cui ovvio che non
torni nulla. Nel secondo invece ne crei un secondo.
test è un generatore (usa yeld) e quindi anche un iteratore.
Qui la differenza tra generatori e iteratori
https://stackoverflow.com/questions/2776829/difference-between-pythons-generators-and-iterators
Il 17/09/21 16:57, Valerio Pachera ha scritto:
Buongiorno a tutti, ho notato questa cosa:
#!/usr/bin/env python3
def test():
for x in range(0, 10):
yield x
iterator = test()
print(test)
for x in iterator:
print(x)
for x in iterator:
print(x)
-------------
Restituisce
<function test at 0x7f17dd3e81f0>
0
1
2
3
4
5
6
7
8
9
-----------
Mentre se modifico lo script con
for x in test():
print(x)
for x in test():
print(x)
-------
Ottengo
<function test at 0x7f7e59baf1f0>
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
-------
Nella mia testa pensavo fosse indifferente richiamare "iterator"
anziché "test()".
Se verifico il tipo di dato ottengo
iterator = test()
print(type(test))
<class 'function'>
------
Non mi è chiarissimo il motivo per cui, al secondo ciclo for non viene
restituito alcun dato.
Sapete darmi una dritta?
_______________________________________________
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python
_______________________________________________
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python