python generator

70 views
Skip to first unread message

bree...@gmail.com

unread,
Dec 11, 2018, 12:29:03 AM12/11/18
to python-tulip
code show as below:
---------------------------------------------------------------------------------------
def generator():
while 1:
recv=yield a
print('recv :',recv)
if __name__=='__main__':
g=generator()

>>> a=10
>>> g.send(None)
10
>>> g.send(None)
recv : None
10
>>> a=11
>>> g.send('next')
recv : next
11
>>> 
-------------------------------------------------------------------------------------------
Why do you claim that its internal variable a, even if it is not declared, does not report an error, and can access the external variable of the same name --a. This makes me puzzled. Can anyone tell me why?

Reply all
Reply to author
Forward
0 new messages