对Python生成器、装饰器、递归的使用详解

yipeiwu_com6年前Python基础

1、Python生成器表达式

1)、Python生成器表达式

语法格式:

(expr for iter_var in iterable)

(expr for iter_var in iterable ifcond_expr)

2)、自定义生成器

函数中使用yield,会返回一个生成器对象。yieldx

生成器使用示例:

In [1]:list((i**2 for i in range(1,11)))

Out[1]:[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

In [2]:def genNum(x):

 ...:  y = 0

 ...:  while y <= x:

 ...:    yield y

 ...:    y += 1

In [3]: g1= genNum(4)

In [4]:type(g1)

Out[4]:generator

In [5]:g1.next()

Out[5]:0

In [6]:g1.next()

Out[6]:1

In [7]:g1.next()

Out[7]:2

In [8]:g1.next()

Out[8]:3

In [9]:g1.next()

Out[9]:4

In [10]:g1.next()

--------------------------------------------------

StopIteration      Traceback (most recent call last)

in()

----> 1g1.next()

StopIteration:

In [11]:def genNum(n):

 ....:  i = 1

 ....:  while i <= n:

 ....:    yield i ** 2

 ....:    i += 1

In [12]:g1 = genNum(20)

In [13]:for i in g1:

 ....:  print i,

 ....:  

1 4 9 1625 36 49 64 81 100 121 144 169 196 225 256 289 324 361400

2、Python装饰器

1)、装饰器本身是一个函数,用于装饰其它函数;

2)、功能:增强被装饰函数的功能;

装饰器一般接受一个函数对象作为参数,以对其进行增强

例1:装饰器使用示例

In [1]:def decorater(func):

 ...:  def wrapper():

 ...:    print "Just a Decorater!"

 ...:    func()

 ...:    raw_input('Please Input your name:')

 ...:  return wrapper

 ...:

In [2]:@decorater

  ...:def show():

 ...:  print "I am from China."

 ...:  

In [3]:show()

Just aDecorater!

I am fromChina.

PleaseInput your name:Fieldyang

例2:对能够传入参数的函数进行装饰

In [1]:def decorater(func):

 ...:  def wrapper(x):

 ...:    print "Just a Decorater!"

 ...:    func(x)

 ...:    raw_input('Please Input your name:')

 ...:  return wrapper

 ...:

In [2]:@decorater

  ...:def show(x):

 ...:  print "I am from China.%s" %x

 ...:  

In [3]:show('how are you ?')

Just aDecorater!

I am fromChina.how are you ?

PleaseInput your name:Fieldyang

3、Python递归

递归需要边界条件,递归前进段和递归返回段;

    10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 *1

    10 * (10-1)* ((10-1)-1)* ...

递归函数使用示例:

In [1]:def recursion(n):

 ...:  if n <= 1: return 1

 ...:  else: return n * recursion(n-1)

 ...: 

#递归函数相当于如下过程:

In [2]: recursion(3) = 3 * recursion(2)= 3 * 2 *recursion(1)=3*2*1

KeyboardInterrupt

In [3]:recursion(3)

Out[3]:6

In [4]:recursion(4)

Out[4]:24

In [5]:recursion(5)

Out[5]:120

In [6]:recursion(10)

Out[6]:3628800

以上这篇对Python生成器、装饰器、递归的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python获取网络图片方法及整理过程详解

这篇文章主要介绍了python获取网络图片方法及整理过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 方式1 使用urllib库...

python识别图像并提取文字的实现方法

python识别图像并提取文字的实现方法

前言 python图像识别一般基础到的就是tesseract了,在爬虫中处理验证码广泛使用。 安装 安装教程网上大都差不多,Windows下确实比较麻烦,涉及到各种路径、环境变量甚至与l...

python基础教程之对象和类的实际运用

我们熟悉了对象和类的基本概念。我们将进一步拓展,以便能实际运用对象和类。 调用类的其它信息 上一讲中提到,在定义方法时,必须有self这一参数。这个参数表示某个对象。对象拥有类的所有性质...

详细介绍Python进度条tqdm的使用

详细介绍Python进度条tqdm的使用

前言 有时候在使用Python处理比较耗时操作的时候,为了便于观察处理进度,这时候就需要通过进度条将处理情况进行可视化展示,以便我们能够及时了解情况。这对于第三方库非常丰富的Python...

python之django母板页面的使用

其实就是利用{% block xxx %}   {% endblock %}的方式定义一个块,相当于占位。存放在某个html中,比如base.html 然后在需要实现...