Advertisements
Advertisements
प्रश्न
Describe what the following module is doing:
c = start
while True:
value = yield c
if value != None:
c = value
else:
c += 1कोड लेखन
Advertisements
उत्तर
This defines a generator that produces a sequence of values beginning with start.
- The first
next()returnsstart. - If a value is sent using
send(value), the generator replacescwith that value. - If
Noneis sent, or ordinarynext()is used,cis increased by one. - It continues indefinitely until the generator is closed.
Example:
def test(start=0):
c = start
while True:
value = yield c
if value is not None:
c = value
else:
c += 1
g = test(5)
print(next(g)) # 5
print(next(g)) # 6
print(g.send(20)) # 20
print(next(g)) # 21shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
