zoukankan      html  css  js  c++  java
  • yield

    x='dddd'
    print x
    print type(x)
    def fun1(Str):
       yield Str
    y=fun1('123')
    print y
    print type(y)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    dddd
    <type 'str'>
    <generator object fun1 at 0x0258F490>
    <type 'generator'>
    
    --------------------------------------------------------------------------------------
    
    x='dddd'
    print x
    print type(x)
    def fun1(Str):
       return Str
    y=fun1('123')
    print y
    print type(y)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    dddd
    <type 'str'>
    123
    <type 'str'>
    
    Process finished with exit code 0
    
    
    
    
    def frange(start, stop, increment):
       x = start
       while x < stop:
        yield x
        x += increment
    b= frange(0, 4, 0.5)
    print b
    print type(b)
    
    def frange(start, stop, increment):
       x = start
       while x < stop:
        yield x
        x += increment
    b= frange(0, 4, 0.5)
    print b
    print type(b)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    dddd
    <type 'str'>
    <generator object fun1 at 0x0248F490>
    <type 'generator'>
    
    
    一个函数中需要有一个  yield 语句即可将其转换为一个生成器。 跟普通函数不同的是,
    生成器只能用于迭代操作。 下面是一个实验,向你展示这样的函数底层工作机制
    
    
    def countdown(n):
       print('Starting to count from', n)
       while n > 0:
       yield n
       n -= 1
       print('Done!')
     # Create the generator, notice no output appears
    c=countdown(10)
    print c
    pritn type(c)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a36.py
    dddd
    <type 'str'>
    <generator object fun1 at 0x024CF490>
    <type 'generator'>
    
    
    --------------------------------------------------------------------------------------------
    def countdown(n):
       print('Starting to count from', n)
       while n > 0:
         yield n
         n -= 1
       print('Done!')
    # Create the generator, notice no output appears
    c=countdown(3)
    print c
    print type(c)
    print next(c)
    print next(c)
    print next(c)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a38.py
    <generator object countdown at 0x025EF4E0>
    <type 'generator'>
    ('Starting to count from', 3)
    3
    2
    1
    
    
    
    如果改为return:
    
    def countdown(n):
       print('Starting to count from', n)
       while n > 0:
         return n
         n -= 1
       print('Done!')
    # Create the generator, notice no output appears
    c=countdown(3)
    print c
    print type(c)
    print next(c)
    # print next(c)
    # print next(c)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a38.py
    ('Starting to count from', 3)
    3
    <type 'int'>
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a38.py", line 11, in <module>
        print next(c)
    TypeError: int object is not an iterator
    
    Process finished with exit code 1
    
    一个生成器函数主要特征是它只会回应在迭代中使用到的 next 操作。 一旦生成器函数返
    回退出,迭代终止。我们在迭代中通常使用的for语句会自动处理这些细节,所以你无需
    担心。
    

  • 相关阅读:
    用了它,你不再羡慕别人家的数据可视化效果多好看
    数据可视化分析除了需要编码的Python,还有更简单的方式吗?
    【实战】SpringBoot + KafKa实现生产者和消费者功能
    利用org.mybatis.generator生成实体类
    企业有了ERP为什么还需要大数据分析,它能为企业解决什么问题?
    四款最受欢迎的大数据可视化工具
    教您通过NBI大数据可视化工具零编码轻松实现桑基图的制作
    用它来做数据可视化分析,真的很简单,老板夸我效率高
    分享一款简单易用的大数据可视化分析工具
    大数据平台常见开源工具有哪些?
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349466.html
Copyright © 2011-2022 走看看