Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite21a558

Browse files
committed
Add generator object demonstration
1 parentdd0da44 commite21a558

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎GeneratorObject/src/generatorobj.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Created on Aug 6, 2017
3+
4+
@author: Aditya
5+
6+
This program explains the details of generator objects.
7+
'''
8+
9+
classinclusive_range:
10+
def__init__(self,*args):# this method is constructor
11+
nargs=len(args)
12+
ifnargs<1:raiseTypeError('Requires atleast one argument.')
13+
elifnargs==1:
14+
self.stop=args[0]
15+
self.start=0
16+
self.step=1
17+
elifnargs==2:
18+
self.start,self.stop=args
19+
self.step=1
20+
elifnargs==3:
21+
self.start,self.stop,self.step=args
22+
else:raiseTypeError('Expected atmost 3 arguments. Got {} arguments.'.format(nargs))
23+
24+
def__iter__(self):
25+
# this method creates the iterator object and we can thus put our generator function over here.
26+
i=self.start
27+
whilei<=self.stop:
28+
yieldi
29+
i+=self.step
30+
31+
defmain():
32+
o=range(25)# range object
33+
print('Type of range object: ',type(o))
34+
foriino:print(i,end=' ')# range object used in context of iterator
35+
print('\nNote: range object is a generator.')
36+
print('Also range is exclusive iterator, i.e., it excludes the upper limit.')
37+
38+
print()
39+
print('Illustrating Generator Object in Python.')
40+
o=inclusive_range(25)
41+
foriino:print(i,end=' ')
42+
print()
43+
44+
if__name__=='__main__':main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp