一、函数知识
1、函数定义与调用
函数的定义形式:
1
2
3
4
5
6
7
|
def
'函数名'
(参数
1
,参数
1
,参数n):
函数功能体
return
"结果"
#可选
#样例函数
def
hello(n):
print
(
"Hello %s"
%
n)
return
"hello() finish."
|
说明:
函数名和变量的命名规则类似,这里不详述;参数可以没有,也可以有多个,默认参数等,参数的类型这里不讨论;可能有return返回计算结果,一个函数也可以没有return,如果没有return语句,则默认返回"None".
样例函数中,在交互下输入hello函数名 或print(hello)会出现类似 <function hello at 0x01FBDEF0>这可以理解成是hello函数在内存中的地址空间即函数自身; 而hello("world")则hello函数的调用,会出现"Hello world";print(hello("world")) 会出现 "Hello world"和 "hello finish." 如图:
2、return 与print区别
在明白了函数的定义和调用以后,来说说print和return区别;
两者主要区别 :print是打印内容,可以出现在任何地方,而return只出现在函数中,用来返回函数运行计算的结果(传给其他函数),同时结束函数,return返回的结果需要print显示才能打印出来;理论上任何一个函数都有一个返回值,如果没有明确return 结果,函数默认返回None。
例子:
1
2
3
4
5
|
def
hi():
print
(
"I am hi."
)
def
hello():
return
(
"I am hello."
)
print
(
"I can't appear!"
)
|
调用hi() 打印出 "I am hi."
调用hello() 没有任何显示,调用结果是返回字符串"I am hello." 不过没有打印出来,如果想hello()调用显示出来需要print(hello()) ,别外 print("I can't appear!")这一句是不会执行的,原由就是如上所说,在碰到return后函数就结束了;而如此同时print(hi()) 则默认return "None"如下所示:
二、函数闭包
定义:在函数内部对嵌套层函数进行返回操作,如下:
1
2
3
4
|
def
hello():
def
hi():
print
(
"hi in hello!"
)
return
hi
#第4行
|
说明:我们调用hello()函数时,没有结果,print(hello()) 同样是返回hi函数本身
调用hello()() 才是类似单独调用定义的hi调用,结果如下:
注意第4行,这里要是写成hi() 即hi函数的调用那么 ,hello()时就会出现 结果"hi in hello!"
hello()()就会出错啦~这里请注意,原因就是本文一开始就说明的函数与函数调用即returun返回了hi调用结果,返回了"hi in hello!",赶紧动手试试吧!
三、函数装饰器
定义:在不改变某一函数(outer)的情况下,增强另一个函数的功能(inner),这种在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator)。
1
2
3
4
5
6
7
8
9
|
def
outer(func):
def
inner():
print
(
"inner is start!"
)
func()
print
(
"inner is finished!"
)
return
inner
def
hello():
print
(
"Hello World!"
)
|
说明:outer就是一个类似闭包函数,不同是outer提供了一个接口(参数,这个参数是函数就是加强的功能)因此可以通过 以下方式使用outer函数:
1、传入hello函数(参数):
print("*" *10)
outer(hello)()
2、传入hello函数并赋给A,调用A()
print("*" *10)
A = outer(hello)
A()
3、赋给同名hello变量,覆盖原来的hello函数
类似 于hello = hello + outer,新的hello整合了hello ,outer的功能
print("*" *10)
hello = outer(hello)
hello()
4、装饰器的写法
@outer
def hello():
print("Hello World2!") #这里为了区分上面的hello
等同于hello = outer(hello)
5、增强outer功能
@outer
def hi():
print("Hi,I am hi!")
print("*" *10)
hi()
6、函数带参数的装饰器
以上的函数比较简单没有带任何参数,那么要是被装饰的函数带参数呢?
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def
outer(f):
def
inner(
*
args,
*
*
kargs):
print
(
"inner is start!"
)
f(
*
args,
*
*
kargs)
print
(
"inner is finish!"
)
return
inner
@outer
def
hi(
*
args,
*
*
kargs):
for
name
in
args:
print
(
"hi %s"
%
name)
print
(kargs)
hi(
"san"
,
"test"
,san
=
18
)
|
运行以上代码结果如下:
7、装饰器带参数用法
模拟需求:
以上的函数和装装饰器基础上要求在不改变函数功能的情况下,当函数执行时通过装饰器传参数,可控记录调用日志。即,当传递”true“时记录日志,什么也不传就不记录日志。
分析:在不改变函数情况下,改装饰器,以上的装装饰器只给函数(带参数的函数)扩充功能,能否直接在装饰器@outer后加参数传递呢?如果看到这里明白了装饰器的核心就会知道,以上例子中直接加参数是不行,@outer("参数")是行不通的 ,因为核心@outer 等同于func = outer(func) 所以直接@outer("参数") 是不可以的。实现的方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
def
loger(flag
=
"flash"
):
def
outer(func):
def
inner():
print
(
"inner is start!"
)
func()
print
(
"inner is finished!"
)
if
flag
=
=
"true"
:
print
(
"%s 函数调用日志已经记录"
%
func.__name__)
return
inner
return
outer
@loger
()
def
hello():
print
(
"Hello World!"
)
@loger
(
"true"
)
def
hello2():
print
(
"Hello2 World!"
)
hello()
print
(
"****分割线****"
*
5
)
hello2()
|
运行结果如图:
可以看到通过在原有的装饰器函数中加一层,这一层主要目的就是类似闭包功能用来向原有装饰器函数中传递变量(参数)。
以上是本人总结,如有错误之处欢迎指正!