Sqrt Iterable example 2
This commit is contained in:
parent
fc8f0baf83
commit
2478a4b4f1
2 changed files with 39 additions and 0 deletions
34
python-temel/list.SqrtIterable.py
Normal file
34
python-temel/list.SqrtIterable.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
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 math.sqrt(self.i)
|
||||
|
||||
s = SqrtIterable(25)
|
||||
|
||||
for f in s:
|
||||
print(f)
|
||||
|
||||
l = list(SqrtIterable(100))
|
||||
|
||||
for f in l:
|
||||
print(f)
|
||||
print()
|
5
python-temel/list.SqrtIterable.py~
Normal file
5
python-temel/list.SqrtIterable.py~
Normal file
|
@ -0,0 +1,5 @@
|
|||
l = list(SqrtIterable(100))
|
||||
|
||||
for f in l:
|
||||
print(f)
|
||||
print()
|
Loading…
Reference in a new issue