9.26学习内容

# 命名关键字参数: 在定义函数时,*与**之间参数称之为命名关键字参数
# 特点:
# 在调用函数时,命名关键字参数必须按照key=value的形式传值
#
# def func(x,*,y=1,z):
# print(x)
# print(y)
# print(z)
#
# func(1,z=2)
#
#
# def func(a,b=2,*args,c,**kwargs):
# print(a)
# print(b)
# print(args)
# print(c)
# print(kwargs)

# 函数是第一类对象: 指的是函数的内存地址可以像一个变量值一样去使用
#
def foo(): #foo=函数的内地址
print('from foo')
#
# 1. 变量值可以被引用
# x=1 #foo=函数的内地址
# y=x

# f=foo
# print(f)
# f()
#
# 2. 变量值可以当作参数传给另外一个函数
# def bar(x):
# print(x)
# x()
#
# x=11111 #foo=函数的内存地址
# bar(x)
# bar(foo)
#
# 3. 变量值可以当作函数的返回值
# def func(x):
# return x
#
# f=func(foo)
# print(f)
#
# 4. 变量值可以当作容器类型的元素
# l=[foo,]
# print(l)
# l[0]()
#
dic={'1':foo}
print(dic)
dic['1']()
#
# def register():
# print('注册....')
#
# def login():
# print('登录....')
#
# def pay():
# print('支付....')
#
# def transfer():
# print('转账....')
#
# func_dic={
# '1':register,
# '2':login,
# '3':pay,
# '4':transfer
# }
#
# func_dic['1']()
#
# while True:
# print("""
# 0 退出
# 1 注册
# 2 登录
# 3 支付
# 4 转账
# """)
# choice=input('请输入你的操作: ').strip()
# if choice == '0':break
#
# if choice not in func_dic:
# print('输错的指令不存在')
# continue
#
# func_dic[choice]()

# 函数的嵌套调用:在一个函数内部又调用其他函数
# def max2(x,y):
# if x > y:
# return x
# else:
# return y
#
# def max4(a,b,c,d):
# res1=max2(a,b)
# res2=max2(res1,c)
# res3=max2(res2,d)
# return res3
#
# print(max4(1,2,3,4))
#
# 函数的嵌套定义: 在函数内又定义了其他函数
def func():
def foo():
print('from foo')
print(foo)
foo()
x=1
print(x)

func()
#
#
# from math import pi
#
# def circle(radius,action):
# def cal_perimeter():
# return 2 * pi * radius
#
# def cal_area():
# return pi * (radius ** 2)
#
# if action == 1:
# res=cal_perimeter()
# elif action == 2:
# res=cal_area()
# return res
#
# res=circle(10,1)
# print(res)
#
#

# '''
# 一 名称空间相关
# 1. 名称空间Namespaces:指的就是存放名字与值内存地址绑定关系的地方(内存空间)
# x=1
#
# 2. 名称空间分为三大类
# 内置名称空间: 存放的是python解释器自带的名字
# 产生:python解释器的启动则产生
# 销毁:python解释器关闭则销毁
#
# 全局名称空间: 在顶级定义的名字
# x=1
# if True:
# y=2
# while True:
# while True:
# while True:
# z=3
# def func():
# pass
#
# 产生:执行python程序时产生
# 销毁:python程序执行完毕后则销毁
#
#
# 局部名称空间: 在函数内定义的名字
# def foo():
# m=100
#
# foo()
# 产生: 在函数调用时临时产生
# 销毁: 在函数调用完毕后则销毁
#
#
# 三种名称空间的产生的先后顺序: 内置->全局->局部
# 查找名字的顺序:从当前位置往外一层一层查找
# 如果当前在局部名称空间: 局部->全局->内置
# 如果当前在全局名称空间: 全局->内置
#
#
# # len=111
# # def foo():
# # # len=222
# # print(len)
# # len=111
# # foo()
#
#
# x=0
# def f1():
# x=1
# def f2():
# x=2
# def f3():
# x=3
# print(x)
# f3()
# f2()
# f1()
# #
#
# def foo1():
# def foo2():
# def foo3():
# print(x)
#
# '''
#
# 二 作用域:指的是作用范围
# 全局作用域:包含内置与全局名称空间的名字
# 特点:全局存活,全局有效
# 局部作用域:包含局部名称空间的名字
# 特点:临时存活,局部有效
# '''
# #全局作用域:包含的是内置名称空间与全局名称空间的名字,
# #特点
# #1在任何位置都能够访问的到
# #2该范围内的名字会伴随程序整个生命周期
#
# #局部作用域:包含的是局部名称空间的名字
# #特点:
# #1、只能在函数内使用
# #2、调用函数时生效,调用结束失效
# '''
#
# !!!作用域关系是在函数定义阶段就已经固定死了,与调用位置无关
# 示范一:
# def f1():
# print(xxx)
# xxx=111
#
# def f2():
# xxx=222
# f1()
#
# f2()
#
# 示范二:
xxx=111
yyy=333
def f1():
xxx=222
print(xxx)
# xxx=222
yyy=222
print(yyy)

f1()

闭包函数:
# 闭:封闭,指的是该函数是定义一个函数内部的函数
# 包:该内部函数包含对外层函数名字的引用
#
# def outter():
# x=1
# def inner():
# print('from inner',x)
# return inner
#
#
# f=outter()
#
# def foo():
# # print(f)
# x=111111111111111111111111111111111111
# f()
# foo()
#
#
# 为函数体传值的两种方式:
# def foo():
# print('hello %s' %name)
#
# 方式一:直接以参数的形式传入
# def foo(name):
# print('hello %s' %name)
#
# foo('egon')
# foo('egon')
# foo('egon')
#
# 方式二:闭包函数
# def outter(name):
# # name='egon'
# def foo():
# print('hello %s' %name)
# return foo
#
# f=outter('egon')
# # print(f)
# f()
# f()
# f()
#
# f1=outter('alex')
# f1()
# f1()
# f1()
#
#
# pip3 install requests
# import requests
#
# 问题
# def get():
# response=requests.get(url)
# if response.status_code == 200:
# print(response.text)
#
# 解决方案一:
# def get(url):
# response=requests.get(url)
# if response.status_code == 200:
# print(response.text)
#
# get('https://www.baidu.com')
# get('https://www.baidu.com')
# get('https://www.baidu.com')
#
# 解决方案二:
#
# def outter(url):
# # 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')
# cnblogs=outter('https://www.cnblogs.com')
#

原文地址:https://www.cnblogs.com/jutao/p/9709904.html