各位用户为了找寻关于Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】的资料费劲了很多周折。这里教程网为您整理了关于Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】的相关资料,仅供查阅,以下为您介绍关于Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】的详细内容
本文实例讲述了Python函数基础用法。分享给大家供大家参考,具体如下:
一、什么是命名关键字参数?
格式: 在*后面参数都是命名关键字参数。
特点:
1、约束函数的调用者必须按照Kye=value的形式传值。
2、约束函数的调用者必须用我们指定的Key名。
? 1 2 3 4 5 6def
auth(
*
args,name,pwd):
print
(name,pwd)
auth(pwd
=
'213'
,name
=
'egon'
)
def
register(name,age):
print
(
type
(name),
type
(age))
register(
123
,[
1
,
2
,
3
])
以上输出:
egon 213 <class 'int'> <class 'list'>
二、函数的嵌套
1、函数的嵌套调用:在函数内又调用了其他函数
? 1 2 3 4 5 6 7 8 9 10def
max2(x,y):
if
x > y:
return
x
else
:
return
y
def
max3(x,y,z):
res1
=
max
(x,y)
res2
=
max
(res1,z)
return
res2
print
(max3(
88
,
99
,
100
))
以上输出:
100
2、函数的嵌套定义:在函数内又定义其他函数。
? 1 2 3 4 5 6 7 8 9def
func1():
print
(
'from func1'
)
def
func2():
#func2=内存地址
print
(
'from func2'
)
print
(func2)
#<function func1.<locals>.func2 at 0x0000024907A098C8>
func2()
func2()
func2()
func1()
以上输出:
from func2 from func2 from func2
三、函数的名称空间
1、名称空间:存放名字与值绑定关系的地方
? 1 2 3x
=
888888888
def
func():
pass
2、名称空间分为三类
(1)内置名称空间:存放python解释器自带的名字,在解释器启动时就生效,解释器关闭则失效。
(2)全局名称空间:文件级别的名字,在执行文件的时候生效,在文件结束或者在文件执行期间被删除则失效。
? 1 2 3 4 5 6 7 8 9 10x
=
1
def
f1():
def
f2():
print
(x)
f2()
f1()
if
10
>
3
:
y
=
33333
while
True
:
xxxxx
=
123123123
以上输出:
1
(3)局部名称空间:存放函数自定义的名字(函数的参数以及函数内的名字都存放与局部名称空间),在函数调用时临时生效,函数结束则失效。
注意:
加载顺序:内置名称空间-------->>全局名称空间------->>>局部名称空间
查找名字:局部名称空间-------->>全局名称空间------->>>内置名称空间
? 1 2 3 4 5 6 7def
f1():
# len=1
def
f2():
# len=2
print
(
len
)
f2()
f1()
以上输出:
global
3、作用域
全局作用域:包涵的是内置名称空间与全局名称空间的名字。
特点:
(1)在任何位置都能够访问的到 (2)该范围内的名字会伴随程序整个生命周期局部作用域:包含的是局部名称空间的名字
特点:
(1)只在函数内使用 (2)调用函数时生效,调用结束失效四、函数对象
1、函数在python中是第一类对象
(1)可以被引用
? 1 2 3 4 5 6x
=
1
y
=
x
def
bar():
print
(
'from bar'
)
f
=
bar
f()
以上输出:
from bar
(2)可以当做参数传入
? 1 2 3 4x
=
1
def
func(a):
print
(a)
func(x)
以上输出:
1
(3)可以当做函数的返回值
代码(1)
? 1 2 3 4 5x
=
1
def
foo():
return
x
res
=
foo()
print
(res)
以上输出:
1
代码(2)
? 1 2 3 4 5 6 7 8def
bar():
print
(
'from bar'
)
def
foo(func):
#func=<function bar at 0x00000225AF631E18>
return
func
#return <function bar at 0x00000225AF631E18>
# print(bar)
f
=
foo(bar)
#f=<function bar at 0x00000225AF631E18>
# print(f)
f()
以上输出:
from bar
(4)可以当做容器类型的元素
? 1 2 3 4 5 6 7 8 9 10x
=
1
l
=
[x,]
print
(l)
def
get():
print
(
'from get'
)
def
put():
print
(
'from put'
)
l
=
[get,put]
# print(l)
l[
0
]()
以上输出:
[1] from get
注意:
1、 func可以被引用
? 1f
=
func
2、func可以当做参数传给x
3、func还可以当做返回值
4、可以当做容器中类型的元素
函数查询登录功能的引用:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33def
auth():
print
(
'请登录:'
)
def
reigster():
print
(
'注册:'
)
def
search():
print
(
'查看:'
)
def
transfer():
print
(
'转账'
)
def
pay():
print
(
'支付'
)
dic
=
{
'1'
:auth,
'2'
:reigster,
'3'
:search,
'4'
:transfer,
'5'
:pay
}
def
interactive():
while
True
:
print
(
'''
1 认证
2 注册
3 查看
4 转账
5 支付
'''
)
choice
=
input
(
'请输入编号:'
).strip()
if
choice
in
dic:
dic[choice]()
else
:
print
(
'非法操作'
)
interactive()
五、闭包函数
闭:指的是定义在函数内部的函数
作用域关系,在函数定义阶段就规定死了,与调用位置无关
? 1 2 3 4 5 6 7 8 9 10 11def
outter():
x
=
2
def
inner():
# x=1
print
(
'from inner'
,x)
return
inner
f
=
outter()
#f=inner
def
foo():
x
=
1111111111111111111111111111
f()
foo()
以上输出:
from inner 2
1、闭包函数:
(1)定义在函数内部的函数
(2)并且该函数包含对外部函数作用域中名字的引用,该函数就称为闭包函数
了解:
为函数体传值的方式
方式一:将值以参数的形式的传入
利用爬虫获取网站的源代码:
? 1 2 3 4 5 6import
requests:
def
get(url):
response
=
requests.get(url)
if
response.status_code
=
=
200
:
print
(response.text)
get(
'https://www.baidu.com'
)
方式二
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15import
requests
import
time
def
outter(url):
#url='https://www.baidu.com'
# url='https://www.baidu.com'
def
get():
response
=
requests.get(url)
if
response.status_code
=
=
200
:
print
(response.text)
return
get
baidu
=
outter(
'https://www.baidu.com'
)
python
=
outter(
'https://www.python.org'
)
baidu()
print
(
'=====================>'
)
time.sleep(
3
)
baidu()
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://www.cnblogs.com/lyox/p/8665386.html