•        Python : 3.7.3
  •          OS : Ubuntu 18.04.2 LTS
  •         IDE : pycharm-community-2019.1.3
  •       Conda : 4.7.5
  •    typesetting : Markdown

code

"""
@Author : 行初心
@Date : 2019/7/6
@Blog : www.cnblogs.com/xingchuxin
@Gitee : gitee.com/zhichengjiu
""" def yield_test():
# next() 遇到yield会停止,保存数据并返回后面的数值
# send_value默认为None
a = 1
send_value = yield a + 1
print(send_value) send_value = yield a + 1
print(send_value) def main():
# 获得生成器
generator = yield_test() # 启动 - 第一种方式
print(id(generator), generator.__next__()) # 想理解好 send 的功能,建议使用 Debug - Step Into(F7)。一步一步地跟下来
print(generator.send("hello")) print("---") # 获得生成器
generator = yield_test() # 启动 - 第二种方式
print(id(generator), generator.send(None))
print(generator.send("world")) print("---") if __name__ == '__main__':
main()

result

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/NumericalComputation/demo.py
140581898176336 2
hello
2
---
140581898176456 2
world
2
--- Process finished with exit code 0

source_code

class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
@abstractmethod
def __next__(self) -> _T_co: ... @abstractmethod
def send(self, value: _T_contra) -> _T_co: ... @abstractmethod
def throw(self, typ: Type[BaseException], val: Optional[BaseException] = ...,
tb: Optional[TracebackType] = ...) -> _T_co: ... @abstractmethod
def close(self) -> None: ... @abstractmethod
def __iter__(self) -> Generator[_T_co, _T_contra, _V_co]: ... @property
def gi_code(self) -> CodeType: ...
@property
def gi_frame(self) -> FrameType: ...
@property
def gi_running(self) -> bool: ...
@property
def gi_yieldfrom(self) -> Optional[Generator]: ... # TODO: Several types should only be defined if sys.python_version >= (3, 5):
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.

more_knowledge

code

"""
@Author : 行初心
@Date : 2019/7/6
@Blog : www.cnblogs.com/xingchuxin
@Gitee : gitee.com/zhichengjiu
""" def yield_test():
a = 1
send_value = yield a + 1
print(send_value) def main():
# 获得生成器
generator = yield_test()
# 直接用send("hello world")
generator.send("hello world") if __name__ == '__main__':
main()

result

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/NumericalComputation/demo.py
Traceback (most recent call last):
File "/home/coder/PycharmProjects/NumericalComputation/demo.py", line 29, in <module>
main()
File "/home/coder/PycharmProjects/NumericalComputation/demo.py", line 25, in main
generator.send("hello world")
TypeError: can't send non-None value to a just-started generator Process finished with exit code 1

reference

resource

  • [文档 - English] docs.python.org/3
  • [文档 - 中文] docs.python.org/zh-cn/3
  • [规范] www.python.org/dev/peps/pep-0008
  • [规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源码] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平台] www.cnblogs.com
  • [平台] gitee.com


Python具有开源、跨平台、解释型、交互式等特性,值得学习。

Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

Python3基础 yield send 获得生成器后,需要先启动一次的更多相关文章

  1. Python3基础 yield send 变量名= yield i

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  2. Python3基础 yield next 获取生成器生出的值

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  3. Python3基础 yield 创建生成器

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  4. Python3基础 yield 在函数中的用法示例

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. Python3基础 yield 在无返回值的函数中的简单示例

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  6. Python3基础 yield StopIteration.value获取函数的返回值

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  7. Python3基础笔记--生成器

    目录: 一.列表生成器 二.生成器 三.迭代器 一.列表生成器 a = [x for x in range(10)] b= [y*2 for y in range(10)] def f(n) retu ...

  8. 理解Python协程:从yield/send到yield from再到async/await

    Python中的协程大概经历了如下三个阶段:1. 最初的生成器变形yield/send2. 引入@asyncio.coroutine和yield from3. 在最近的Python3.5版本中引入as ...

  9. 5 Python3 函数进阶&迭代器与生成器

    1.函数进阶 1.1.名称空间 又名name space, 顾名思义就是存放名字的地方,存什么名字呢?举例说明,若变量x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的 ...

随机推荐

  1. QQ推广工具

    目前比较简单易用的QQ推广工具有:一键加群.在线聊天 一.一键加群 1.官网链接 http://qun.qq.com/join.html 2.使用 1登录自己的QQ 2创建一个想要作为推广的群 3选择 ...

  2. matplotlib绘图难题解决

    # coding=utf-8 import pandas as pd import yagmail import requests import arrow import numpy as np im ...

  3. 云计算与大数据实验:Hbase shell操作用户表

    [实验目的] 1)了解hbase服务 2)学会hbase shell命令操作用户表 [实验原理] HBase是一个分布式的.面向列的开源数据库,它利用Hadoop HDFS作为其文件存储系统,利用Ha ...

  4. linux下的 c 和 c++ 开发工具及linux内核开发工具

    https://opensource.com/article/18/6/embedded-linux-build-tools https://github.com/luong-komorebi/Awe ...

  5. 基于kubeamd初始化安装kubernetes集群

    环境:master,etcd 172.16.1.5node1 172.16.1.6node2 172.16.1.7前提:1.基于主机名通信,/etc/hosts 2.时间同步3.关闭firewalld ...

  6. python开发总结

    1.思维缜密的编程逻辑 2.满足明确的目的需求 3.运用现成的轮子加以改造 4.学会装饰自己的程序 5.化繁为简 6.多用配置文件作为入口 7.注意扩展兼容

  7. shortcuts for contructor 创建对象捷径

  8. CSE301 – Bio-Computation

    CSE301 – Bio-Computation Assessment 3Contribution to overall module assessment 10%Submission deadlin ...

  9. nginx设置反向代理,获取真实客户端ip

    upstream这个模块提供一个简单方法来实现在轮询和客户端IP之间的后端服务器负荷平衡. upstream abc.com { server 127.0.0.1:8080; server 127.0 ...

  10. python zlib模块缺失报错:RuntimeError: Compression requires the (missing) zlib module

    解决方式: # yum install zlib # yum install zlib-devel 下载成功后,进入python2.7的目录,重新执行 #make #make install 此时先前 ...