FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonLearning/47.IteratorsInPython.py at Pythontuts · aditya0035/PythonLearning · GitHub
aditya0035
/
PythonLearning
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PythonLearning
/
47.IteratorsInPython.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (32 loc) · 1013 Bytes
Breadcrumbs
PythonLearning
/
47.IteratorsInPython.py
Copy path
File metadata and controls
35 lines (32 loc) · 1013 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Iterators are those which will implement __next__ method then any object can call next on them
"""
class
FirstHundreadNumber
:
def
__init__
(
self
):
self
.
number
=
0
def
__next__
(
self
):
if
self
.
number
<=
100
:
current
=
self
.
number
self
.
number
=
self
.
number
+
1
return
current
else
:
"""
These will raise stop iteration to stop the iteration once completed
"""
raise
StopIteration
()
#it tell the python that we have reach the end of generator
instance
=
FirstHundreadNumber
()
print
(
next
(
instance
))
print
(
next
(
instance
))
print
(
next
(
instance
))
"""
These class will behave like generator but are iterator and call be called only via next method
Not all the iterators are generators
"""
#lst=list(instance) #does not work as not iterable
#print(list)
"""
in python 2 we create iterators using next method implementation
"""
"""
Also we can't call for each on iterators
"""
Back
|
FazBrowse Home
|
New Git URL