की विधि भेजें मैं send
विधि को समझ नहीं पा रहा हूं। मैं समझता हूं कि इसका उपयोग जनरेटर को संचालित करने के लिए किया जाता है। लेकिन वाक्यविन्यास यहां है: generator.send(value)
।पायथन 3: जेनरेटर
मैं किसी भी तरह पकड़ नहीं सकता कि मूल्य वर्तमान yield
अभिव्यक्ति का परिणाम क्यों बनना चाहिए।
def gen():
for i in range(10):
X = yield i
if X == 'stop':
break
print("Inside the function " + str(X))
m = gen()
print("1 Outside the function " + str(next(m)) + '\n')
print("2 Outside the function " + str(next(m)) + '\n')
print("3 Outside the function " + str(next(m)) + '\n')
print("4 Outside the function " + str(next(m)) + '\n')
print('\n')
print("Outside the function " + str(m.send(None)) + '\n') # Start generator
print("Outside the function " + str(m.send(77)) + '\n')
print("Outside the function " + str(m.send(88)) + '\n')
#print("Outside the function " + str(m.send('stop')) + '\n')
print("Outside the function " + str(m.send(99)) + '\n')
print("Outside the function " + str(m.send(None)) + '\n')
परिणाम है::
1 Outside the function 0
Inside the function None
2 Outside the function 1
Inside the function None
3 Outside the function 2
Inside the function None
4 Outside the function 3
Inside the function None
Outside the function 4
Inside the function 77
Outside the function 5
Inside the function 88
Outside the function 6
Inside the function 99
Outside the function 7
Inside the function None
Outside the function 8
ठीक है, स्पष्ट रूप से बोल रहा है, यह मेरे आश्चर्यजनक है मैं एक उदाहरण तैयार किया।
- प्रलेखन हम पढ़ सकते हैं कि जब एक
yield
बयान निष्पादित किया जाता है, जनरेटर के राज्य जमे हुए है औरexpression_list
का मूल्यnext
के फोन करने वाले के लिए वापस आ जाता है। ठीक है, ऐसा प्रतीत नहीं होता है। हमgen()
के अंदरif
कथन औरprint
फ़ंक्शन निष्पादित क्यों कर सकते हैं। - मैं कैसे समझ सकता हूं कि
X
फ़ंक्शन के अंदर और बाहर क्यों भिन्न है? ठीक है। आइए मान लें किsend(77)
77 मेंm
प्रसारित करता है। खैर,yield
अभिव्यक्ति 77 हो जाती है। फिरX = yield i
क्या है? और समारोह के अंदर 77 कैसे बाहर होता है जब बाहर होता है? - पहला परिणाम स्ट्रिंग जनरेटर के अंदर जो भी हो रहा है उसे प्रतिबिंबित नहीं करती है?
वैसे भी, क्या आप किसी भी तरह इन send
और yield
कथन पर टिप्पणी कर सकते हैं?
को मान असाइन। – jfs
@ जेएफ। सेबेस्टियन मुझे लगता है कि "समवर्ती" का अर्थ समानांतर में है? – ecatmur
प्रोग्रामिंग "समांतर" में "समवर्ती" का तात्पर्य है लेकिन सामान्य में विपरीत सत्य नहीं है। [समवर्ती प्रोग्रामिंग और समांतर प्रोग्रामिंग के बीच अंतर] (http://stackoverflow.com/q/1897993/4279) – jfs