Definizione di funzioni generatore
This commit is contained in:
@@ -12,10 +12,31 @@ class MyIterator:
|
|||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
|
|
||||||
|
def get_double_gen():
|
||||||
|
e = 2
|
||||||
|
while e < 300:
|
||||||
|
yield e
|
||||||
|
e *= 2
|
||||||
|
|
||||||
|
|
||||||
|
def get_double_gen2():
|
||||||
|
e = 2
|
||||||
|
while True:
|
||||||
|
yield e
|
||||||
|
e *= 2
|
||||||
|
if e >= 300:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
print("Iterators with class MyIterator")
|
||||||
for x in MyIterator():
|
for x in MyIterator():
|
||||||
print(x)
|
print(x)
|
||||||
|
|
||||||
|
print("\nIterators with function get_double_gen")
|
||||||
|
for x in get_double_gen():
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user