Python学习笔记 for windows 二
函数
abs(-20) //结果为:20,绝对值函数
def 函数名称([参数1,参数2,参数3]):
执行语句
return
def test(r):
s=3.14*r*r
return s
s=test(1)
print s //结果为:3.14
cmp(x,y) //比较函数,如果x<y
,返回-1
,如果x==y
,返回0
,如果x>y
,返回1
float('12.34') //结果为:12.34
str(123) //结果为:'123'
unicode(100) //结果为:u'100'
bool(1) //结果为:True
bool('') //结果为:False
空语句 pass ,相当于占位符,让代码先跑起来
据类型检查可以用内置函数isinstance实现:
isinstance
if not isinstance(x, (int, float)): raise TypeError('bad operand type')
函数可以同时返回多个值,但其实就是一个tuple。
def test(a):
x=a+1
y=a+2
return x,y
r=test(1)
print r //结果为(2,3)
def enroll(name,gender,age=6,city='BeiJing'): //参数后面加等号即为默认参数,调取函数时选填
print 'name:',name
print 'gender:',gender
print 'age:',age
print 'city:'city
enroll('Ling','F')
enroll('ling','M',7)
enroll('ling','M',city='ShangHai')
默认参数如用到[],会有个坑,重复调用会记住上次的[],写的时候最好这么写
def add_end(L=None) //使用None这个不变的对象来实现
if L is None:
L=[]
.....
可变参数
def calc(numbers): sum = 0 for n in numbers: sum = sum + n * n return sum
但是调用时需要先组装出一个list或者tuple
calc([1, 2, 3])
calc((1, 3, 5, 7))
在参数numbers前面加个*,就变为可变参数,调用时就不须组装list和tuple
def calc(*numbers):
calc(1,2,3)
calc()
此时若已有list或者tuple,可以这样调取
nums=[1,2,3]
calc(*nums)
关键字参数
def person(name, age, **kw): print 'name:', name, 'age:', age, 'other:', kw
person('Michael', 30) //结果为:name: Michael age: 30 other: {}
kw = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **kw) //结果为:name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}
要注意定义可变参数和关键字参数的语法:
*args是可变参数,args接收的是一个tuple;
**kw是关键字参数,kw接收的是一个dict。
以及调用函数时如何传入可变参数和关键字参数的语法:
可变参数既可以直接传入:func(1, 2, 3),又可以先组装list或tuple,再通过*args传入:func(*(1, 2, 3));
关键字参数既可以直接传入:func(a=1, b=2),又可以先组装dict,再通过**kw传入:func(**{'a': 1, 'b': 2})。
使用*args和**kw是Python的习惯写法,当然也可以用其他参数名,但最好使用习惯用法。
递归函数
def fact(n): if n==1: return 1 return n * fact(n - 1)
使用递归函数需要注意防止栈溢出。在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出
解决递归调用栈溢出的方法是通过尾递归优化,事实上尾递归和循环的效果是一样的,所以,把循环看成是一种特殊的尾递归函数也是可以的。
尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。
切片
L=[1,2,3,4,5,6,7,8]
L[0:3] //结果为:[1,2,3],从索引0开始取,直到取到索引3,但不包括索引3
L[:3] //结果为:[1,2,3],索引0可省略
L[-8:] //结果为:[1,2,3,4,5,6,7,8],从倒数第八个开始取,冒号后省略就取到最后一个
L[::2] //结果为:[1,3,5,7],第三个参数是每隔几个取一个
'ABCDEFG'[1:5:2] //结果为:'BD',可对字符串进行切片
calc([1, 2, 3])
calc((1, 3, 5, 7))
def calc(*numbers):
calc(1,2,3)
calc()
此时若已有list或者tuple,可以这样调取
nums=[1,2,3]
calc(*nums)
关键字参数
def person(name, age, **kw): print 'name:', name, 'age:', age, 'other:', kw
person('Michael', 30) //结果为:name: Michael age: 30 other: {}
kw = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **kw) //结果为:name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}
要注意定义可变参数和关键字参数的语法:
*args是可变参数,args接收的是一个tuple;
**kw是关键字参数,kw接收的是一个dict。
以及调用函数时如何传入可变参数和关键字参数的语法:
可变参数既可以直接传入:func(1, 2, 3),又可以先组装list或tuple,再通过*args传入:func(*(1, 2, 3));
关键字参数既可以直接传入:func(a=1, b=2),又可以先组装dict,再通过**kw传入:func(**{'a': 1, 'b': 2})。
使用*args和**kw是Python的习惯写法,当然也可以用其他参数名,但最好使用习惯用法。
递归函数
def fact(n): if n==1: return 1 return n * fact(n - 1)
使用递归函数需要注意防止栈溢出。在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出
解决递归调用栈溢出的方法是通过尾递归优化,事实上尾递归和循环的效果是一样的,所以,把循环看成是一种特殊的尾递归函数也是可以的。
尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。
切片
L=[1,2,3,4,5,6,7,8]
L[0:3] //结果为:[1,2,3],从索引0开始取,直到取到索引3,但不包括索引3
L[:3] //结果为:[1,2,3],索引0可省略
L[-8:] //结果为:[1,2,3,4,5,6,7,8],从倒数第八个开始取,冒号后省略就取到最后一个
L[::2] //结果为:[1,3,5,7],第三个参数是每隔几个取一个
def calc(*numbers):
calc(1,2,3)
calc()
def person(name, age, **kw): print 'name:', name, 'age:', age, 'other:', kw
person('Michael', 30) //结果为:name: Michael age: 30 other: {}
kw = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **kw) //结果为:name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}
*args
**kw
func(1, 2, 3)
*args
func(*(1, 2, 3))
func(a=1, b=2)
**kw
func(**{'a': 1, 'b': 2})
*args
**kw
def fact(n): if n==1: return 1 return n * fact(n - 1)
迭代
如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration)。
默认情况下,dict迭代的是key:
d = {'a': 1, 'b': 2, 'c': 3} for key in d: print key a c b
如果要迭代value可以用for value in d.itervalues(),如果要同时迭代key和value,可以用for k, v in d.iteritems()。
字符串也可迭代
那么,如何判断一个对象是可迭代对象呢?方法是通过collections模块的Iterable类型判断:
>>> from collections import Iterable >>> isinstance('abc', Iterable) # str是否可迭代 True >>> isinstance([1,2,3], Iterable) # list是否可迭代 True
最后一个小问题,如果要对list实现类似Java那样的下标循环怎么办?Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身:
>>> for i, value in enumerate(['A', 'B', 'C']): ... print i, value ... 0 A 1 B 2 C
列表生成式
range(1,11)即为list[1,2,3,4,5,6,7,8,9,10]
>>> [x * x for x in range(1, 11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
for循环后面还可以加上if判断,这样我们就可以筛选出仅偶数的平方:
>>> [x * x for x in range(1, 11) if x % 2 == 0] [4, 16, 36, 64, 100]
还可以使用两层循环,可以生成全排列:
>>> [m + n for m in 'ABC' for n in 'XYZ'] ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
>>> d = {'x': 'A', 'y': 'B', 'z': 'C' } >>> [k + '=' + v for k, v in d.iteritems()] ['y=B', 'x=A', 'z=C']
生成器
通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。
所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器(Generator)。
要创建一个generator,有很多种方法。第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个generator:
>>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> g = (x * x for x in range(10)) >>> g <generator object <genexpr> at 0x104feab40>
创建L和g的区别仅在于最外层的[]和(),L是一个list,而g是一个generator。
我们可以直接打印出list的每一个元素,但我们怎么打印出generator的每一个元素呢?
如果要一个一个打印出来,可以通过generator的next()方法:
>>> g.next() 0 >>> g.next() 1 >>> g.next() 4 >>> g.next() 9 >>> g.next() 16 >>> g.next() 25 >>> g.next() 36 >>> g.next() 49 >>> g.next() 64 >>> g.next() 81
我们讲过,generator保存的是算法,每次调用next(),就计算出下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出StopIteration的错误。
当然,上面这种不断调用next()方法实在是太变态了,正确的方法是使用for循环,因为generator也是可迭代对象:
>>> g = (x * x for x in range(10)) >>> for n in g: ... print n ... 0 1 4 9 16 25 36 49 64 81
斐波拉契数列用列表生成式写不出来,但是,用函数把它打印出来却很容易:
def fib(max): n, a, b = 0, 0, 1 while n < max: print b a, b = b, a + b n = n + 1
>>> fib(6) 1 1 2 3 5 8
仔细观察,可以看出,fib函数实际上是定义了斐波拉契数列的推算规则,可以从第一个元素开始,推算出后续任意的元素,这种逻辑其实非常类似generator。
也就是说,上面的函数和generator仅一步之遥。要把fib函数变成generator,只需要把print b改为yield b就可以了:
def fib(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n = n + 1
这就是定义generator的另一种方法。如果一个函数定义中包含yield关键字,那么这个函数就不再是一个普通函数,而是一个generator:
>>> fib(6) <generator object fib at 0x104feaaa0>
这里,最难理解的就是generator和函数的执行流程不一样。函数是顺序执行,遇到return语句或者最后一行函数语句就返回。而变成generator的函数,在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行。
举个简单的例子,定义一个generator,依次返回数字1,3,5:
>>> def odd(): ... print 'step 1' ... yield 1 ... print 'step 2' ... yield 3 ... print 'step 3' ... yield 5 ... >>> o = odd() >>> o.next() step 1 1 >>> o.next() step 2 3 >>> o.next() step 3 5
变量可指向变量
f=abs
f(-10)结果为10
既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
map/reduce
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。
如
def f(x):
return x*x
map(f,range(10))
map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])
再看reduce的用法。reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
比方说对一个序列求和,就可以用reduce实现:
>>> def add(x, y): ... return x + y ... >>> reduce(add, [1, 3, 5, 7, 9]) 25
首字母大写 'LING'.capitalize()类似的还有大小写upper(),lower()
for
d = {'a': 1, 'b': 2, 'c': 3} for key in d: print key a c b
for value in d.itervalues()
for k, v in d.iteritems()
>>> from collections import Iterable >>> isinstance('abc', Iterable) # str是否可迭代 True >>> isinstance([1,2,3], Iterable) # list是否可迭代 True
enumerate
for
>>> for i, value in enumerate(['A', 'B', 'C']): ... print i, value ... 0 A 1 B 2 C
列表生成式
range(1,11)即为list[1,2,3,4,5,6,7,8,9,10]
>>> [x * x for x in range(1, 11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
for循环后面还可以加上if判断,这样我们就可以筛选出仅偶数的平方:
>>> [x * x for x in range(1, 11) if x % 2 == 0] [4, 16, 36, 64, 100]
还可以使用两层循环,可以生成全排列:
>>> [m + n for m in 'ABC' for n in 'XYZ'] ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
>>> d = {'x': 'A', 'y': 'B', 'z': 'C' } >>> [k + '=' + v for k, v in d.iteritems()] ['y=B', 'x=A', 'z=C']
生成器
通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。
所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器(Generator)。
要创建一个generator,有很多种方法。第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个generator:
>>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> g = (x * x for x in range(10)) >>> g <generator object <genexpr> at 0x104feab40>
创建L和g的区别仅在于最外层的[]和(),L是一个list,而g是一个generator。
我们可以直接打印出list的每一个元素,但我们怎么打印出generator的每一个元素呢?
如果要一个一个打印出来,可以通过generator的next()方法:
>>> g.next() 0 >>> g.next() 1 >>> g.next() 4 >>> g.next() 9 >>> g.next() 16 >>> g.next() 25 >>> g.next() 36 >>> g.next() 49 >>> g.next() 64 >>> g.next() 81
我们讲过,generator保存的是算法,每次调用next(),就计算出下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出StopIteration的错误。
当然,上面这种不断调用next()方法实在是太变态了,正确的方法是使用for循环,因为generator也是可迭代对象:
>>> g = (x * x for x in range(10)) >>> for n in g: ... print n ... 0 1 4 9 16 25 36 49 64 81
斐波拉契数列用列表生成式写不出来,但是,用函数把它打印出来却很容易:
def fib(max): n, a, b = 0, 0, 1 while n < max: print b a, b = b, a + b n = n + 1
>>> fib(6) 1 1 2 3 5 8
仔细观察,可以看出,fib函数实际上是定义了斐波拉契数列的推算规则,可以从第一个元素开始,推算出后续任意的元素,这种逻辑其实非常类似generator。
也就是说,上面的函数和generator仅一步之遥。要把fib函数变成generator,只需要把print b改为yield b就可以了:
def fib(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n = n + 1
这就是定义generator的另一种方法。如果一个函数定义中包含yield关键字,那么这个函数就不再是一个普通函数,而是一个generator:
>>> fib(6) <generator object fib at 0x104feaaa0>
这里,最难理解的就是generator和函数的执行流程不一样。函数是顺序执行,遇到return语句或者最后一行函数语句就返回。而变成generator的函数,在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行。
举个简单的例子,定义一个generator,依次返回数字1,3,5:
>>> def odd(): ... print 'step 1' ... yield 1 ... print 'step 2' ... yield 3 ... print 'step 3' ... yield 5 ... >>> o = odd() >>> o.next() step 1 1 >>> o.next() step 2 3 >>> o.next() step 3 5
变量可指向变量
f=abs
f(-10)结果为10
既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
>>> [x * x for x in range(1, 11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
for循环后面还可以加上if判断,这样我们就可以筛选出仅偶数的平方:
>>> [x * x for x in range(1, 11) if x % 2 == 0] [4, 16, 36, 64, 100]
还可以使用两层循环,可以生成全排列:
>>> [m + n for m in 'ABC' for n in 'XYZ'] ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
>>> d = {'x': 'A', 'y': 'B', 'z': 'C' } >>> [k + '=' + v for k, v in d.iteritems()] ['y=B', 'x=A', 'z=C']
[]
()
>>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> g = (x * x for x in range(10)) >>> g <generator object <genexpr> at 0x104feab40>
L
g
[]
()
L
g
next()
>>> g.next() 0 >>> g.next() 1 >>> g.next() 4 >>> g.next() 9 >>> g.next() 16 >>> g.next() 25 >>> g.next() 36 >>> g.next() 49 >>> g.next() 64 >>> g.next() 81
next()
next()
for
>>> g = (x * x for x in range(10)) >>> for n in g: ... print n ... 0 1 4 9 16 25 36 49 64 81
def fib(max): n, a, b = 0, 0, 1 while n < max: print b a, b = b, a + b n = n + 1
>>> fib(6) 1 1 2 3 5 8
fib
fib
print b
yield b
def fib(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n = n + 1
yield
>>> fib(6) <generator object fib at 0x104feaaa0>
next()
yield
yield
>>> def odd(): ... print 'step 1' ... yield 1 ... print 'step 2' ... yield 3 ... print 'step 3' ... yield 5 ... >>> o = odd() >>> o.next() step 1 1 >>> o.next() step 2 3 >>> o.next() step 3 5
map()
map
map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
>>> def add(x, y): ... return x + y ... >>> reduce(add, [1, 3, 5, 7, 9]) 25
Python内建的filter()函数用于过滤序列。
和map()类似,filter()也接收一个函数和一个序列。和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
排序算法
filter()
map()
filter()
map()
filter()
True
False
通常规定,对于两个元素x和y,如果认为x < y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1
x
y
x < y
-1
x == y
0
x > y
1
>>> sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36]
此外,sorted()函数也是一个高阶函数,它还可以接收一个比较函数来实现自定义的排序。比如,如果要倒序排序,我们就可以自定义一个reversed_cmp函数:
def reversed_cmp(x, y): if x > y: return -1 if x < y: return 1 return 0
传入自定义的比较函数reversed_cmp,就可以实现倒序排序:
>>> sorted([36, 5, 12, 9, 21], reversed_cmp) [36, 21, 12, 9, 5]
返回函数
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum
当我们调用lazy_sum()时,返回的并不是求和结果,而是求和函数:
>>> f = lazy_sum(1, 3, 5, 7, 9) >>> f <function sum at 0x10452f668>
调用函数f时,才真正计算求和的结果:
>>> f() 25
在这个例子中,我们在函数lazy_sum中又定义了函数sum,并且,内部函数sum可以引用外部函数lazy_sum的参数和局部变量,当lazy_sum返回函数sum时,相关参数和变量都保存在返回的函数中,这种称为“闭包(Closure)”的程序结构拥有极大的威力。
请再注意一点,当我们调用lazy_sum()时,每次调用都会返回一个新的函数,即使传入相同的参数:
>>> f1 = lazy_sum(1, 3, 5, 7, 9) >>> f2 = lazy_sum(1, 3, 5, 7, 9) >>> f1==f2 False
匿名函数
map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
关键字lambda表示匿名函数,冒号前面的x表示函数参数。
匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果。
用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突。
装饰器
def now():
print '2015-3-28'
f=now
f()
f.__name__ //结果为:'now',函数对象有个__name__属性,可以拿到函数的名字
在面向对象(OOP)的设计模式中,decorator被称为装饰模式。OOP的装饰模式需要通过继承和组合来实现,而Python除了能支持OOP的decorator外,直接从语法层次支持decorator。Python的decorator可以用函数实现,也可以用类实现。
decorator可以增强函数的功能,定义起来虽然有点复杂,但使用起来非常灵活和方便。
现在,假设我们要增强now()函数的功能,比如,在函数调用前后自动打印日志,但又不希望修改now()函数的定义,这种在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator)。
本质上,decorator就是一个返回函数的高阶函数。所以,我们要定义一个能打印日志的decorator,可以定义如下:
def log(func): def wrapper(*args, **kw): print 'call %s():' % func.__name__ return func(*args, **kw) return wrapper
观察上面的log,因为它是一个decorator,所以接受一个函数作为参数,并返回一个函数。我们要借助Python的@语法,把decorator置于函数的定义处:
@log def now(): print '2013-12-25'
调用now()函数,不仅会运行now()函数本身,还会在运行now()函数前打印一行日志:
>>> now() call now(): 2013-12-25
把@log放到now()函数的定义处,相当于执行了语句:
now = log(now)
由于log()是一个decorator,返回一个函数,所以,原来的now()函数仍然存在,只是现在同名的now变量指向了新的函数,于是调用now()将执行新函数,即在log()函数中返回的wrapper()函数。
wrapper()函数的参数定义是(*args, **kw),因此,wrapper()函数可以接受任意参数的调用。在wrapper()函数内,首先打印日志,再紧接着调用原始函数。
如果decorator本身需要传入参数,那就需要编写一个返回decorator的高阶函数,写出来会更复杂。
>>> sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36]
sorted()
reversed_cmp
def reversed_cmp(x, y): if x > y: return -1 if x < y: return 1 return 0
传入自定义的比较函数reversed_cmp,就可以实现倒序排序:
>>> sorted([36, 5, 12, 9, 21], reversed_cmp) [36, 21, 12, 9, 5]
返回函数
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum
当我们调用lazy_sum()时,返回的并不是求和结果,而是求和函数:
>>> f = lazy_sum(1, 3, 5, 7, 9) >>> f <function sum at 0x10452f668>
调用函数f时,才真正计算求和的结果:
>>> f() 25
在这个例子中,我们在函数lazy_sum中又定义了函数sum,并且,内部函数sum可以引用外部函数lazy_sum的参数和局部变量,当lazy_sum返回函数sum时,相关参数和变量都保存在返回的函数中,这种称为“闭包(Closure)”的程序结构拥有极大的威力。
请再注意一点,当我们调用lazy_sum()时,每次调用都会返回一个新的函数,即使传入相同的参数:
>>> f1 = lazy_sum(1, 3, 5, 7, 9) >>> f2 = lazy_sum(1, 3, 5, 7, 9) >>> f1==f2 False
匿名函数
map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
关键字lambda表示匿名函数,冒号前面的x表示函数参数。
匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果。
用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突。
装饰器
def now():
print '2015-3-28'
f=now
f()
f.__name__ //结果为:'now',函数对象有个__name__属性,可以拿到函数的名字
在面向对象(OOP)的设计模式中,decorator被称为装饰模式。OOP的装饰模式需要通过继承和组合来实现,而Python除了能支持OOP的decorator外,直接从语法层次支持decorator。Python的decorator可以用函数实现,也可以用类实现。
decorator可以增强函数的功能,定义起来虽然有点复杂,但使用起来非常灵活和方便。
现在,假设我们要增强now()函数的功能,比如,在函数调用前后自动打印日志,但又不希望修改now()函数的定义,这种在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator)。
本质上,decorator就是一个返回函数的高阶函数。所以,我们要定义一个能打印日志的decorator,可以定义如下:
def log(func): def wrapper(*args, **kw): print 'call %s():' % func.__name__ return func(*args, **kw) return wrapper
观察上面的log,因为它是一个decorator,所以接受一个函数作为参数,并返回一个函数。我们要借助Python的@语法,把decorator置于函数的定义处:
@log def now(): print '2013-12-25'
调用now()函数,不仅会运行now()函数本身,还会在运行now()函数前打印一行日志:
>>> now() call now(): 2013-12-25
把@log放到now()函数的定义处,相当于执行了语句:
now = log(now)
由于log()是一个decorator,返回一个函数,所以,原来的now()函数仍然存在,只是现在同名的now变量指向了新的函数,于是调用now()将执行新函数,即在log()函数中返回的wrapper()函数。
wrapper()函数的参数定义是(*args, **kw),因此,wrapper()函数可以接受任意参数的调用。在wrapper()函数内,首先打印日志,再紧接着调用原始函数。
如果decorator本身需要传入参数,那就需要编写一个返回decorator的高阶函数,写出来会更复杂。
reversed_cmp
>>> sorted([36, 5, 12, 9, 21], reversed_cmp) [36, 21, 12, 9, 5]
返回函数
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum
当我们调用lazy_sum()时,返回的并不是求和结果,而是求和函数:
>>> f = lazy_sum(1, 3, 5, 7, 9) >>> f <function sum at 0x10452f668>
调用函数f时,才真正计算求和的结果:
>>> f() 25
在这个例子中,我们在函数lazy_sum中又定义了函数sum,并且,内部函数sum可以引用外部函数lazy_sum的参数和局部变量,当lazy_sum返回函数sum时,相关参数和变量都保存在返回的函数中,这种称为“闭包(Closure)”的程序结构拥有极大的威力。
请再注意一点,当我们调用lazy_sum()时,每次调用都会返回一个新的函数,即使传入相同的参数:
>>> f1 = lazy_sum(1, 3, 5, 7, 9) >>> f2 = lazy_sum(1, 3, 5, 7, 9) >>> f1==f2 False
匿名函数
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum
lazy_sum()
>>> f = lazy_sum(1, 3, 5, 7, 9) >>> f <function sum at 0x10452f668>
f
>>> f() 25
lazy_sum
sum
sum
lazy_sum
lazy_sum
sum
lazy_sum()
>>> f1 = lazy_sum(1, 3, 5, 7, 9) >>> f2 = lazy_sum(1, 3, 5, 7, 9) >>> f1==f2 False
map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
关键字lambda表示匿名函数,冒号前面的x表示函数参数。
匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果。
用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突。
装饰器
def now():
map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
lambda
x
return
print '2015-3-28'
now()
now()
def log(func): def wrapper(*args, **kw): print 'call %s():' % func.__name__ return func(*args, **kw) return wrapper
log
@log def now(): print '2013-12-25'
now()
now()
now()
>>> now() call now(): 2013-12-25
@log
now()
now = log(now)
log()
now()
now()
log()
wrapper()
wrapper()
(*args, **kw)
wrapper()
wrapper()
一个完整的decorator的写法如下:
import functools def log(func): @functools.wraps(func) def wrapper(*args, **kw): print 'call %s():' % func.__name__ return func(*args, **kw) return wrapper
import functools def log(text): def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): print '%s %s():' % (text, func.__name__) return func(*args, **kw) return wrapper return decorator
import functools是导入functools模块。模块的概念稍候讲解。现在,只需记住在定义wrapper()的前面加上@functools.wraps(func)即可。
偏函数
functools.partial就是帮助我们创建一个偏函数的,不需要我们自己定义int2(),可以直接使用下面的代码创建一个新的函数int2:
>>> import functools >>> int2 = functools.partial(int, base=2) >>> int2('1000000') 64 >>> int2('1010101') 85
所以,简单总结functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。
import functools def log(func): @functools.wraps(func) def wrapper(*args, **kw): print 'call %s():' % func.__name__ return func(*args, **kw) return wrapper
import functools def log(text): def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): print '%s %s():' % (text, func.__name__) return func(*args, **kw) return wrapper return decorator
import functools
functools
wrapper()
@functools.wraps(func)
functools.partial
int2()
int2
>>> import functools >>> int2 = functools.partial(int, base=2) >>> int2('1000000') 64 >>> int2('1010101') 85
functools.partial
模块
#!/usr/bin/env python # -*- coding: utf-8 -*- ' a test module ' __author__ = 'Michael Liao' import sys def test(): args = sys.argv if len(args)==1: print 'Hello, world!' elif len(args)==2: print 'Hello, %s!' % args[1] else: print 'Too many arguments!' if __name__=='__main__': test()
第1行和第2行是标准注释,第1行注释可以让这个hello.py文件直接在Unix/Linux/Mac上运行,第2行注释表示.py文件本身使用标准UTF-8编码;
第4行是一个字符串,表示模块的文档注释,任何模块代码的第一个字符串都被视为模块的文档注释;
第6行使用__author__变量把作者写进去,这样当你公开源代码后别人就可以瞻仰你的大名;
以上就是Python模块的标准文件模板,当然也可以全部删掉不写,但是,按标准办事肯定没错。
后面开始就是真正的代码部分。
你可能注意到了,使用sys模块的第一步,就是导入该模块:
import sys
导入sys模块后,我们就有了变量sys指向该模块,利用sys这个变量,就可以访问sys模块的所有功能。
sys模块有一个argv变量,用list存储了命令行的所有参数。argv至少有一个元素,因为第一个参数永远是该.py文件的名称,例如:
运行python hello.py获得的sys.argv就是['hello.py'];
运行python hello.py Michael获得的sys.argv就是['hello.py', 'Michael]。
最后,注意到这两行代码:
if __name__=='__main__': test()
当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if判断将失败,因此,这种if测试可以让一个模块通过命令行运行时执行一些额外的代码,最常见的就是运行测试。
我们可以用命令行运行hello.py看看效果:
$ python hello.py Hello, world! $ python hello.py Michael Hello, Michael!
如果启动Python交互环境,再导入hello模块:
$ python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import hello >>>
导入时,没有打印Hello, word!,因为没有执行test()函数。
调用hello.test()时,才能打印出Hello, word!:
>>> hello.test() Hello, world!
别名
导入模块时,还可以使用别名,这样,可以在运行时根据当前环境选择最合适的模块。比如Python标准库一般会提供StringIO和cStringIO两个库,这两个库的接口和功能是一样的,但是cStringIO是C写的,速度更快,所以,你会经常看到这样的写法:
try: import cStringIO as StringIO except ImportError: # 导入失败会捕获到ImportError import StringIO
这样就可以优先导入cStringIO。如果有些平台不提供cStringIO,还可以降级使用StringIO。导入cStringIO时,用import ... as ...指定了别名StringIO,因此,后续代码引用StringIO即可正常工作。
还有类似simplejson这样的库,在Python 2.6之前是独立的第三方库,从2.6开始内置,所以,会有这样的写法:
try: import json # python >= 2.6 except ImportError: import simplejson as json # python <= 2.5
由于Python是动态语言,函数签名一致接口就一样,因此,无论导入哪个模块后续代码都能正常工作。
如果采用面向对象的程序设计思想,我们首选思考的不是程序的执行流程,而是Student这种数据类型应该被视为一个对象,这个对象拥有name和score这两个属性(Property)。如果要打印一个学生的成绩,首先必须创建出这个学生对应的对象,然后,给对象发一个print_score消息,让对象自己把自己的数据打印出来。
class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print '%s: %s' % (self.name, self.score)
Try
给对象发消息实际上就是调用对象对应的关联函数,我们称之为对象的方法(Method)。面向对象的程序写出来就像这样:
bart = Student('Bart Simpson', 59) lisa = Student('Lisa Simpson', 87) bart.print_score() lisa.print_score()
面向对象的设计思想是从自然界中来的,因为在自然界中,类(Class)和实例(Instance)的概念是很自然的。Class是一种抽象概念,比如我们定义的Class——Student,是指学生这个概念,而实例(Instance)则是一个个具体的Student,比如,Bart Simpson和Lisa Simpson是两个具体的Student:
所以,面向对象的设计思想是抽象出Class,根据Class创建Instance。
面向对象的抽象程度又比函数要高,因为一个Class既包含数据,又包含操作数据的方法。
数据封装、继承和多态是面向对象的三大特点。
类和实例
#!/usr/bin/env python # -*- coding: utf-8 -*- ' a test module ' __author__ = 'Michael Liao' import sys def test(): args = sys.argv if len(args)==1: print 'Hello, world!' elif len(args)==2: print 'Hello, %s!' % args[1] else: print 'Too many arguments!' if __name__=='__main__': test()
hello.py
__author__
sys
import sys
sys
sys
sys
sys
sys
argv
argv
python hello.py
sys.argv
['hello.py']
python hello.py Michael
sys.argv
['hello.py', 'Michael]
if __name__=='__main__': test()
hello
__name__
__main__
hello
if
if
hello.py
$ python hello.py Hello, world! $ python hello.py Michael Hello, Michael!
hello
$ python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import hello >>>
Hello, word!
test()
hello.test()
Hello, word!
>>> hello.test() Hello, world!
StringIO
cStringIO
cStringIO
try: import cStringIO as StringIO except ImportError: # 导入失败会捕获到ImportError import StringIO
cStringIO
cStringIO
StringIO
cStringIO
import ... as ...
StringIO
StringIO
simplejson
try: import json # python >= 2.6 except ImportError: import simplejson as json # python <= 2.5
Student
name
score
print_score
class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print '%s: %s' % (self.name, self.score)
bart = Student('Bart Simpson', 59) lisa = Student('Lisa Simpson', 87) bart.print_score() lisa.print_score()
class Student(object): pass
class
Student
(object)
object
Student
Student
Student
>>> bart = Student() >>> bart <__main__.Student object at 0x10a67a590> >>> Student <class '__main__.Student'>
bart
0x10a67a590
Student
bart
name
>>> bart.name = 'Bart Simpson' >>> bart.name 'Bart Simpson'
__init__
name
score
class Student(object): def __init__(self, name, score): self.name = name self.score = score
__init__
self
__init__
self
self
__init__
__init__
self
>>> bart = Student('Bart Simpson', 59) >>> bart.name 'Bart Simpson' >>> bart.score 59
self
Student
name
score
>>> def print_score(std): ... print '%s: %s' % (std.name, std.score) ... >>> print_score(bart) Bart Simpson: 59
Student
Student
Student
class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print '%s: %s' % (self.name, self.score)
self
self
>>> bart.print_score() Bart Simpson: 59
Student
name
score
Student
Student
get_grade
class Student(object): ... def get_grade(self): if self.score >= 90: return 'A' elif self.score >= 60: return 'B' else: return 'C'
get_grade
>>> bart.get_grade() 'C'
>>> bart = Student('Bart Simpson', 59) >>> lisa = Student('Lisa Simpson', 87) >>> bart.age = 8 >>> bart.age 8 >>> lisa.age Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'age'
name
score
>>> bart = Student('Bart Simpson', 98) >>> bart.score 98 >>> bart.score = 59 >>> bart.score 59
__
__
class Student(object): def __init__(self, name, score): self.__name = name self.__score = score def print_score(self): print '%s: %s' % (self.__name, self.__score)
实例变量.__name
实例变量.__score
>>> bart = Student('Bart Simpson', 98) >>> bart.__name Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute '__name'
get_name
get_score
class Student(object): ... def get_name(self): return self.__name def get_score(self): return self.__score
set_score
class Student(object): ... def set_score(self, score): self.__score = score
bart.score = 59
class Student(object): ... def set_score(self, score): if 0 <= score <= 100: self.__score = score else: raise ValueError('bad score')
__xxx__
__name__
__score__
_name
__name
__name
_Student__name
_Student__name
__name
>>> bart._Student__name 'Bart Simpson'
__name
Animal
run()
class Animal(object): def run(self): print 'Animal is running...'
class Dog(Animal): pass class Cat(Animal): pass
run()
run()
dog = Dog() dog.run() cat = Cat() cat.run()
Animal is running... Animal is running...
class Dog(Animal): def run(self): print 'Dog is running...' def eat(self): print 'Eating meat...'
run()
Animal is running...
Dog is running...
Cat is running...
class Dog(Animal): def run(self): print 'Dog is running...' class Cat(Animal): def run(self): print 'Cat is running...'
Dog is running... Cat is running...
run()
run()
run()
run()
a = list() # a是list类型 b = Animal() # b是Animal类型 c = Dog() # c是Dog类型
isinstance()
>>> isinstance(a, list) True >>> isinstance(b, Animal) True >>> isinstance(c, Dog) True
>>> isinstance(c, Animal) True
c
c
c
>>> b = Animal() >>> isinstance(b, Dog) False
def run_twice(animal): animal.run() animal.run()
run_twice()
>>> run_twice(Animal()) Animal is running... Animal is running...
run_twice()
>>> run_twice(Dog()) Dog is running... Dog is running...
run_twice()
>>> run_twice(Cat()) Cat is running... Cat is running...
class Tortoise(Animal): def run(self): print 'Tortoise is running slowly...'
>>> run_twice(Tortoise()) Tortoise is running slowly... Tortoise is running slowly...
run()
run()
run()
run()
run()
run_twice()
type()
type()
>>> type(123) <type 'int'> >>> type('str') <type 'str'> >>> type(None) <type 'NoneType'>
type()
>>> type(abs) <type 'builtin_function_or_method'> >>> type(a) <class '__main__.Animal'>
type()
if
>>> type(123)==type(456) True >>> type('abc')==type('123') True >>> type('abc')==type(123) False
types
>>> import types >>> type('abc')==types.StringType True >>> type(u'abc')==types.UnicodeType True >>> type([])==types.ListType True >>> type(str)==types.TypeType True
TypeType
TypeType
>>> type(int)==type(str)==types.TypeType True
isinstance()
object -> Animal -> Dog -> Husky
isinstance()
>>> a = Animal() >>> d = Dog() >>> h = Husky()
>>> isinstance(h, Husky) True
h
>>> isinstance(h, Dog) True
h
h
isinstance()
h
>>> isinstance(h, Animal) True
d
>>> isinstance(d, Dog) and isinstance(d, Animal) True
d
>>> isinstance(d, Husky) False
type()
isinstance()
>>> isinstance('a', str) True >>> isinstance(u'a', unicode) True >>> isinstance('a', unicode) False
>>> isinstance('a', (str, unicode)) True >>> isinstance(u'a', (str, unicode)) True
str
unicode
basestring
>>> isinstance(u'a', basestring) True
dir()
>>> dir('ABC') ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
__xxx__
__len__
len()
len()
__len__()
>>> len('ABC') 3 >>> 'ABC'.__len__() 3
len(myObj)
__len__()
>>> class MyObject(object): ... def __len__(self): ... return 100 ... >>> obj = MyObject() >>> len(obj) 100
lower()
>>> 'ABC'.lower() 'abc'
getattr()
setattr()
hasattr()
>>> class MyObject(object): ... def __init__(self): ... self.x = 9 ... def power(self): ... return self.x * self.x ... >>> obj = MyObject()
>>> hasattr(obj, 'x') # 有属性'x'吗? True >>> obj.x 9 >>> hasattr(obj, 'y') # 有属性'y'吗? False >>> setattr(obj, 'y', 19) # 设置一个属性'y' >>> hasattr(obj, 'y') # 有属性'y'吗? True >>> getattr(obj, 'y') # 获取属性'y' 19 >>> obj.y # 获取属性'y' 19
>>> getattr(obj, 'z') # 获取属性'z' Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'MyObject' object has no attribute 'z'
>>> getattr(obj, 'z', 404) # 获取属性'z',如果不存在,返回默认值404 404
>>> hasattr(obj, 'power') # 有属性'power'吗? True >>> getattr(obj, 'power') # 获取属性'power' <bound method MyObject.power of <__main__.MyObject object at 0x108ca35d0>> >>> fn = getattr(obj, 'power') # 获取属性'power'并赋值到变量fn >>> fn # fn指向obj.power <bound method MyObject.power of <__main__.MyObject object at 0x108ca35d0>> >>> fn() # 调用fn()与调用obj.power()是一样的 81
sum = obj.x + obj.y
sum = getattr(obj, 'x') + getattr(obj, 'y')
def readImage(fp): if hasattr(fp, 'read'): return readData(fp) return None
hasattr()
read()
read()
>>> class Student(object): ... pass ...
>>> s = Student() >>> s.name = 'Michael' # 动态给实例绑定一个属性 >>> print s.name Michael
>>> def set_age(self, age): # 定义一个函数作为实例方法 ... self.age = age ... >>> from types import MethodType >>> s.set_age = MethodType(set_age, s, Student) # 给实例绑定一个方法 >>> s.set_age(25) # 调用实例方法 >>> s.age # 测试结果 25
>>> s2 = Student() # 创建新的实例 >>> s2.set_age(25) # 尝试调用方法 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'set_age'
>>> def set_score(self, score): ... self.score = score ... >>> Student.set_score = MethodType(set_score, None, Student)
>>> s.set_score(100) >>> s.score 100 >>> s2.set_score(99) >>> s2.score 99
set_score
name
age
__slots__
>>> class Student(object): ... __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称 ...
>>> s = Student() # 创建新的实例 >>> s.name = 'Michael' # 绑定属性'name' >>> s.age = 25 # 绑定属性'age' >>> s.score = 99 # 绑定属性'score' Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'score'
'score'
__slots__
score
score
__slots__
__slots__
>>> class GraduateStudent(Student): ... pass ... >>> g = GraduateStudent() >>> g.score = 9999
__slots__
__slots__
__slots__
s = Student() s.score = 9999
set_score()
get_score()
set_score()
class Student(object): def get_score(self): return self._score def set_score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!') if value < 0 or value > 100: raise ValueError('score must between 0 ~ 100!') self._score = value
>>> s = Student() >>> s.set_score(60) # ok! >>> s.get_score() 60 >>> s.set_score(9999) Traceback (most recent call last): ... ValueError: score must between 0 ~ 100!
@property
class Student(object): @property def score(self): return self._score @score.setter def score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!') if value < 0 or value > 100: raise ValueError('score must between 0 ~ 100!') self._score = value
@property
@property
@property
@score.setter
>>> s = Student() >>> s.score = 60 # OK,实际转化为s.set_score(60) >>> s.score # OK,实际转化为s.get_score() 60 >>> s.score = 9999 Traceback (most recent call last): ... ValueError: score must between 0 ~ 100!
@property
class Student(object): @property def birth(self): return self._birth @birth.setter def birth(self, value): self._birth = value @property def age(self): return 2014 - self._birth
birth
age
age
birth
@property
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步