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语句会自动处理这些细节,所以你无需
    担心。
    

  • 相关阅读:
    ASP.NET2.0中将文件上传到数据库
    C#中数据类型转换
    数据绑定以及Container.DataItem的具体分析
    CodeSmith开发系列资料总结
    Jquery信息专题收集
    microsoft .net framework专题汇总
    ASP.NET中插入FLASH代码
    javascript专题汇总
    经典SQL行列转换
    前台js调用后台c#方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349466.html
Copyright © 2011-2022 走看看