next method example 2

This commit is contained in:
Mert Gör ☭ 2023-09-03 14:51:41 +03:00
parent 7be4ed70a7
commit a30910c48d
No known key found for this signature in database
GPG Key ID: 2100A876D55B39B9
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import math
class SqrtIterable:
def __init__(self, limit):
self._limit = limit
def __iter__(self):
return SqrtIterator(self._limit)
class SqrtIterator:
def __init__(self, limit):
self._limit = limit
self._i = 0
def __iter__(self):
return self
def __next__(self):
if self._i == self._limit:
raise StopIteration()
self._i += 1
return self._i, math.sqrt(self._i)
si = SqrtIterable(25)
for t in si:
print(t, end = ' ')
print()
for i, x in si:
print(f'{i} --> {x}')