账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python 生成器交互的一个问题?
    25
    0

    在python交互模式下有如下代码:

    def squares(cursor=1):
        response=None
        while True:
            if response:
                response=yield response**2
                continue
            response=yield cursor**2
            cursor+=1
    
    sq=squares()
    >>> next(sq)
    1
    >>> next(sq)
    4
    >>> sq.send(7)
    49
    >>> next(sq)
    9

    问题如下:
    第一次调用next(sq)后,为什么response没有被赋值为1?(因为第二次调用next的结果为4)

    第一次调用sq.send(7)时,response是否被赋值为7?为什么再次调用next结果依然是9?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部