day08-函数进阶
1.参数的补充
在函数基础部分,我们掌握函数和参数基础知识,掌握这些其实完全就可以进行项目的开发。
今天的补充内容属于进阶知识,包含:内存地址相关、面试题相关等,在特定情况下也可以让代码更加简洁,提升开发效率。
1.1 参数内存地址相关【面试题】
在开始讲参数内存地址相关之前,我们先来学习一个技能:
如何查看某个值在内存中的地址?
v1 = "赵四"
add = id(v1)
print(add)
记住一句话:函数执行传参时,传递的是内存地址。
def func(data):
print(data, id(data))
v1 = "赵四"
print(id(v1)) # 2047280120048
func(v1) # 赵四 2047280120048
python参数的这一特性有两个好处:
- 节省内存
- 对于可变类型且函数中修改元素的内容,所有的地方都会修改。可变类型:list、dict、set。
# 可变类型 & 修改内部元素
def func(data):
data.append(666)
data_list = [11, 22, 33]
func(data_list)
print(data_list) # [11, 22, 33, 666]
# 特殊情况:可变类型 & 重新赋值
def func(data):
data = ["赵四", "alex"]
data_list = [11, 22, 33]
func(data_list)
print(data_list) # [11, 22, 33]
# 特殊情况:不可变类型,无法修改内部元素,只能重新赋值
def func(data):
data = "alex"
v1 = "赵四"
func(v1)
print(v1) # 赵四
其他很多编程语言执行函数时,默认传参时会将数据重新拷贝一份,会浪费内存。
当然,如果你不想让外部的变量和函数内部参数的变量一致,也可以选择将外部值拷贝一份,再传给函数。
import copy
def func(data):
data.append(666)
data_list = [11, 22, 33]
new_data = copy.deepcopy(data_list)
func(new_data)
print(data_list) # [11, 22, 33]
1.2 函数的返回值是内存地址
def func():
data = [11, 22, 33]
return data
v1 = func()
print(v1)
上述代码的执行过程:
- 执行func函数
data = [11, 22, 33]
创建一块内存区域,内部存储[11, 22, 33]
,data变量指向这块内存地址。return data
返回data指向的内存地址- v1接收返回值,所以v1和data都指向
[11, 22, 33]
的内存地址(两个变量指向此内存,引用计数器为2) - 由函数执行完毕之后,函数内部的变量会被释放。(即:删除data变量,内存地址的引用计数器-1)
所以,最终v1指向函数内部创建的那块内存地址。
def func():
data = [11, 22, 33]
print(id(data))
return data
v1 = func()
print(id(v1)) # 2121071951488
v2 = func()
print(id(v2)) # 2121072278144
上述代码中v1和v2指向的内存地址不是同一块。
1.3 参数的默认值【面试题】
这个知识点在面试题中出现的概率比较高,但真正实际开发中用的比较少。
def func(a1,a2=18):
print(a1,a2)
原理:Python在创建函数(未执行)时,如果发现函数的参数中有默认值,则在函数内部会创建一块区域并维护这个默认值。
- 执行函数未传值时,则让a2指向 函数维护的那个值的地址。
func("root")
- 执行函数传值时,则让a2指向新传入的值的地址。
func("admin",20)
在特定情况【默认参数的值是可变类型 list/dict/set】 & 【函数内部会修改这个值】下,参数的默认值 有坑 。
-
坑
# 在函数内存中会维护一块区域存储 [1,2,666,666,666] 100010001 def func(a1,a2=[1,2]): a2.append(666) print(a1,a2) # a1=100 # a2 -> 100010001 func(100) # 100 [1,2,666] # a1=200 # a2 -> 100010001 func(200) # 200 [1,2,666,666] # a1=99 # a2 -> 1111111101 func(99,[77,88]) # 99 [77,88,666] # a1=300 # a2 -> 100010001 func(300) # 300 [1,2,666,666,666]
-
大坑
# 在内部会维护一块区域存储 [1, 2, 10, 20,40 ] ,内存地址 1010101010 def func(a1, a2=[1, 2]): a2.append(a1) return a2 # a1=10 # a2 -> 1010101010 # v1 -> 1010101010 v1 = func(10) print(v1) # [1, 2, 10] # a1=20 # a2 -> 1010101010 # v2 -> 1010101010 v2 = func(20) print(v2) # [1, 2, 10, 20] # a1=30 # a2 -> 11111111111 [11, 22,30] # v3 -> 11111111111 v3 = func(30, [11, 22]) print(v3) # [11, 22,30] # a1=40 # a2 -> 1010101010 # v4 -> 1010101010 v4 = func(40) print(v4) # [1, 2, 10, 20, 40]
-
深坑
# 内存中创建空间存储 [1, 2, 10, 20, 40] 地址:1010101010 def func(a1, a2=[1, 2]): a2.append(a1) return a2 # a1=10 # a2 -> 1010101010 # v1 -> 1010101010 v1 = func(10) # a1=20 # a2 -> 1010101010 # v2 -> 1010101010 v2 = func(20) # a1=30 # a2 -> 11111111111 [11,22,30] # v3 -> 11111111111 v3 = func(30, [11, 22]) # a1=40 # a2 -> 1010101010 # v4 -> 1010101010 v4 = func(40) print(v1) # [1, 2, 10, 20, 40] print(v2) # [1, 2, 10, 20, 40] print(v3) # [11,22,30] print(v4) # [1, 2, 10, 20, 40]
1.4 动态参数
动态参数,定义函数时在形参位置用 *或**
可以接任意个参数。
def func(*args,**kwargs):
print(args,kwargs)
func("宝强","杰伦",n1="alex",n2="eric")
在定义函数时可以用 *和**
,其实在执行函数时,也可以用。
-
形参固定,实参用
*和**
def func(a1,a2): print(a1,a2) func( 11, 22 ) func( a1=1, a2=2 ) func( *[11,22] ) func( **{"a1":11,"a2":22} )
-
形参用
*和**
,实参也用*和**
def func(*args,**kwargs): print(args,kwargs) func( 11, 22 ) func( 11, 22, name="武沛齐", age=18 ) # 小坑,([11,22,33], {"k1":1,"k2":2}), {} func( [11,22,33], {"k1":1,"k2":2} ) # args=(11,22,33),kwargs={"k1":1,"k2":2} func( *[11,22,33], **{"k1":1,"k2":2} ) # 值得注意:按照这个方式将数据传递给args和kwargs时,数据是会重新拷贝一份的(可理解为内部循环每个元素并设置到args和kwargs中)。
所以,在使用format字符串格式化时,可以这样:
v1 = "我是{},年龄:{}。".format("武沛齐",18)
v2 = "我是{name},年龄:{age}。".format(name="武沛齐",age=18)
v3 = "我是{},年龄:{}。".format(*["武沛齐",18])
v4 = "我是{name},年龄:{age}。".format(**{"name":"武沛齐","age":18})
练习题
-
看代码写结果
def func(*args,**kwargs): print(args,kwargs) params = {"k1":"v2","k2":"v2"} func(params) # ({"k1":"v2","k2":"v2"}, ) {} func(**params) # (), {"k1":"v2","k2":"v2"}
-
读取文件中的 URL 和 标题,根据URL下载视频到本地(以标题作为文件名)。
模仿,https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f570000bvbmace0gvch7lo53oog&ratio=720p&line=0 卡特,https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f3e0000bv52fpn5t6p007e34q1g&ratio=720p&line=0 罗斯,https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f240000buuer5aa4tij4gv6ajqg&ratio=720p&line=0
# 下载视频示例 import requests res = requests.get( url="https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f240000buuer5aa4tij4gv6ajqg&ratio=720p&line=0", headers={ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS" } ) with open('rose.mp4', mode='wb') as f: f.write(res.content)
import requests import os base_path = os.path.dirname(os.path.abspath(__file__)) target_path = os.path.join(base_path, "movie/") # 设置视频保存的目录 if not os.path.exists(target_path): os.mkdir(target_path) # 根据url地址下载并保存 def dowload_movie(name, url): resp = requests.get(url, headers={ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS"}) with open(os.path.join(target_path, "{}.mp4".format(name)), mode="wb") as f: f.write(resp.content) # 读取文件进行下载 with open(os.path.join(base_path, "files", "movie.csv"), mode="r", encoding="utf-8") as f: movie_info = f.readlines() for item in movie_info: row_list = item.strip().split(",") dowload_movie(*row_list)
2.函数和函数名
函数名其实就是一个变量,这个变量只不过代指的函数而已。
name = "武沛齐"
def add(n1,n2):
return n1 + n2
注意:函数必须先定义才能被调用执行(解释型语言)。
# 正确
def add(n1,n2):
return n1 + n2
ret = add(1,2)
print(ret)
# 错误
ret = add(1,2)
print(ret)
def add(n1,n2):
return n1 + n2
2.1 函数做元素
既然函数就相当于是一个变量,那么在列表等元素中是否可以把行数当做元素呢?
def func():
return 123
data_list = ["武沛齐", "func", func , func() ]
print( data_list[0] ) # 字符串"武沛齐"
print( data_list[1] ) # 字符串 "func"
print( data_list[2] ) # 函数 func
print( data_list[3] ) # 整数 123
res = data_list[2]()
print( res ) # 执行函数 func,并获取返回值;print再输出返回值。
print( data_list[2]() ) # 123
注意:函数同时也可被哈希,所以函数名通知也可以当做 集合的元素、字典的键。
掌握这个知识之后,对后续的项目开发有很大的帮助,例如,在项目中遇到根据选择做不同操作时:
-
情景1,例如:要开发一个类似于微信的功能。
def send_message(): """发送消息""" pass def send_image(): """发送图片""" pass def send_emoji(): """发送表情""" pass def send_file(): """发送文件""" pass print("欢迎使用xx系统") print("请选择:1.发送消息;2.发送图片;3.发送表情;4.发送文件") choice = input("输入选择的序号") if choice == "1": send_message() elif choice == "2": send_image() elif choice == "3": send_emoji() elif choice == "4": send_file() else: print("输入错误")
def send_message(): """发送消息""" pass def send_image(): """发送图片""" pass def send_emoji(): """发送表情""" pass def send_file(): """发送文件""" pass def xxx(): """收藏""" pass function_dict = { "1": send_message, "2": send_image, "3": send_emoji, "4": send_file, "5": xxx } print("欢迎使用xx系统") print("请选择:1.发送消息;2.发送图片;3.发送表情;4.发送文件") choice = input("输入选择的序号") # "1" func = function_dict.get(choice) if not func: print("输入错误") else: # 执行函数 func()
-
情景2,例如:某个特定情况,要实现发送短信、微信、邮件。
def send_msg(): """发送短信""" pass def send_email(): """发送图片""" pass def send_wechat(): """发送微信""" # 执行函数 send_msg() send_email() send_wechat()
def send_msg(): """发送短信""" pass def send_email(): """发送图片""" pass def send_wechat(): """发送微信""" pass func_list = [ send_msg, send_email, send_wechat ] for item in func_list: item()
上述两种情景,在参数相同时才可用,如果参数不一致,会出错。所以,在项目设计时就要让程序满足这一点,如果无法满足,也可以通过其他手段时间,例如:
情景1:
def send_message(phone,content):
"""发送消息"""
pass
def send_image(img_path, content):
"""发送图片"""
pass
def send_emoji(emoji):
"""发送表情"""
pass
def send_file(path):
"""发送文件"""
pass
function_dict = {
"1": [ send_message, ['15131255089', '你好呀']],
"2": [ send_image, ['xxx/xxx/xx.png', '消息内容']],
"3": [ send_emoji, ["😁"]],
"4": [ send_file, ['xx.zip'] ]
}
print("欢迎使用xx系统")
print("请选择:1.发送消息;2.发送图片;3.发送表情;4.发送文件")
choice = input("输入选择的序号") # 1
item = function_dict.get(choice) # [ send_message, ['15131255089', '你好呀']],
if not item:
print("输入错误")
else:
# 执行函数
func = item[0] # send_message
param_list = item[1] # ['15131255089', '你好呀']
func(*param_list) # send_message(*['15131255089', '你好呀'])
情景2:
def send_msg(mobile, content):
"""发送短信"""
pass
def send_email(to_email, subject, content):
"""发送图片"""
pass
def send_wechat(user_id, content):
"""发送微信"""
pass
func_list = [
{"name": send_msg, "params": {'mobile': "15131255089", "content": "你有新短消息"}},
{"name": send_email, "params": {'to_email': "wupeiqi@live.com", "subject": "报警消息", "content": "硬盘容量不够用了"}},
{"name": send_wechat, "params": {'user_id': 1, 'content': "约吗"}},
]
# {"name": send_msg, "params": {'mobile': "15131255089", "content": "你有新短消息"}},
for item in func_list:
func = item['name'] # send_msg
param_dict = item['params'] # {'mobile': "15131255089", "content": "你有新短消息"}
func(**param_dict) # send_msg(**{'mobile': "15131255089", "content": "你有新短消息"})
2.2 函数名赋值
-
将函数名赋值给其他变量,函数名其实就个变量,代指某函数;如果将函数名赋值给另外一个变量,则此变量也会代指该函数,例如:
def func(a1,a2): print(a1,a2) xxxxx = func # 此时,xxxxx和func都代指上面的那个函数,所以都可以被执行。 func(1,1) xxxxx(2,2)
def func(a1,a2): print(a1,a2) func_list = [func,func,func] func(11,22) func_list[0](11,22) func_list[1](33,44) func_list[2](55,66)
-
对函数名重新赋值,如果将函数名修改为其他值,函数名便不再代指函数,例如:
def func(a1,a2): print(a1,a2) # 执行func函数 func(11,22) # func重新赋值成一个字符串 func = "武沛齐" print(func)
def func(a1,a2): print(a1+a2) func(1,2) def func(): print(666) func()
注意:由于函数名被重新定义之后,就会变量新被定义的值,所以大家在自定义函数时,不要与python内置的函数同名,否则会覆盖内置函数的功能,例如:
id,bin,hex,oct,len...
# len内置函数用于计算值得长度 v1 = len("武沛齐") print(v1) # 3 # len重新定义成另外一个函数 def len(a1,a2): return a1 + a2 # 以后执行len函数,只能按照重新定义的来使用 v3 = len(1,2) print(v3)
2.3 函数名做参数和返回值
函数名其实就一个变量,代指某个函数,所以,他和其他的数据类型一样,也可以当做函数的参数和返回值。
-
参数
def plus(num): return num + 100 def handler(func): res = func(10) # 110 msg = "执行func,并获取到的结果为:{}".format(res) print(msg) # 执行func,并获取到的结果为:110 # 执行handler函数,将plus作为参数传递给handler的形式参数func handler(plus)
-
返回值
def plus(num): return num + 100 def handler(): print("执行handler函数") return plus result = handler() data = result(20) # 120 print(data)
3.返回值和print
对于初学者的同学,很多人都对print和返回值分不清楚,例如:
def add(n1,n2):
print(n1 + n2)
v1 = add(1,3)
print(v1)
# 输出
4
None
def plus(a1,a2):
return a1 + a2
v2 = plus(1,2)
print(v2)
# 输出
3
这两个函数是完全不同的
- 在函数中使用print,只是用于在某个位置输出内容而已。
- 在函数中使用return,是为了将函数的执行结果返回给调用者,以便于后续其他操作。
在调用并执行函数时,要学会分析函数的执行步骤。
def f1():
print(123)
def f2(arg):
ret = arg()
return ret
v1 = f2(f1)
print(v1)
# 输出
123
None
def f1():
print(123)
def f2(arg):
ret = arg()
return f1
v1 = f2(f1)
v2 = v1()
print(v2)
# 输出
123
123
None
4. 作用域
作用域,可以理解为一块空间,这块空间的数据是可以共享的。通俗点来说,作用域就类似于一个房子,房子中的东西归里面的所有人共享,其他房子的人无法获取。
4.1 函数为作用域
Python以函数为作用域,所以在函数内创建的所有数据,可以此函数中被使用,无法在其他函数中被使用。
def func():
name = "武沛齐"
data_list = [11,22,33,44]
print(name,data_list)
age = 20
print(age)
def handler():
age = 18
print(age)
func()
handler()
学会分析代码,了解变量到底属于哪个作用域且是否可以被调用:
def func():
name = "武沛齐"
age = 29
print(age) # 29
data_list = [11,22,33,44]
print(name,data_list)
for num in range(10):
print(num)
print(num) # 9
if 1 == 1:
value = "admin"
print(value) # admin
print(value) # admin
if 1 > 2:
max_num = 10
print(max_num)
print(max_num) # 报错
def handler():
age = 18
print(age)
handler()
func()
4.2 全局和局部
Python中以函数为作用域,函数的作用域其实是一个局部作用域。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}
]
for index in range(len(goods)):
item = goods[index]
print(index + 1, item['name'], item['price'])
while True:
num = input("请输入要选择的商品序号(Q/q):") # "1"
if num.upper() == "Q":
break
if not num.isdecimal():
print("用输入的格式错误")
break
num = int(num)
send_email()
if num > 4 or num < 0:
print("范围选择错误")
break
target_index = num - 1
choice_item = goods[target_index]
print(choice_item["name"], choice_item['price'])
send_email()
# 全局变量(变量名大写)
COUNTRY = "中国"
CITY_LIST = ["北京","上海","深圳"]
def download():
# 局部变量
url = "http://www.xxx.com"
...
def upload():
file_name = "rose.zip"
...
COUNTRY
和CITY_LIST
是在全局作用域中,全局作用域中创建的变量称之为【全局变量】,可以在全局作用域中被使用,也可以在其局部作用域中被使用。
download
和upload
函数内部维护的就是一个局部作用域,在各自函数内部创建变量称之为【局部变量】,且局部变量只能在此作用域中被使用。局部作用域中想使用某个变量时,寻找的顺序为:优先在局部作用域中寻找,如果没有则去上级作用域中寻找
。
注意:全局变量一般都是大写。
示例1:在局部作用域中读取全局作用域的变量。
COUNTRY = "中国"
CITY_LIST = ["北京","上海","深圳"]
def download():
url = "http://www.xxx.com"
print(url)
print(COUNTRY)
print(CITY_LIST)
def upload():
file_name = "rose.zip"
print(file_name)
print(COUNTRY)
print(CITY_LIST)
print(COUNTRY)
print(CITY_LIST)
download()
upload()
print(file_name) # 报错
print(url) # 报错
示例2:局部作用域和全局作用域变量同名,这算啥?
COUNTRY = "中国"
CITY_LIST = ["北京","上海","深圳"]
def download():
url = "http://www.xxx.com"
CITY_LIST = ["河北","河南","山西"]
print(url)
print(COUNTRY) # "中国"
print(CITY_LIST) # ["河北","河南","山西"]
def upload():
file_name = "rose.zip"
print(COUNTRY) # "中国"
print(CITY_LIST) # ["北京","上海","深圳"]
print(COUNTRY)
print(CITY_LIST)
download()
upload()
COUNTRY = "中国"
CITY_LIST = ["北京","上海","深圳"]
def download():
url = "http://www.xxx.com"
CITY_LIST = ["河北","河南","山西"]
print(url)
print(COUNTRY)
print(CITY_LIST)
def upload():
file_name = "rose.zip"
print(COUNTRY)
print(CITY_LIST)
print(COUNTRY)
print(CITY_LIST)
download()
upload()
COUNTRY = "中华人民共和共国"
CITY_LIST = [11,22,33]
download()
upload()
# 输出
中国
["北京","上海","深圳"]
http://www.xxx.com
中国
["河北","河南","山西"]
中国
["北京","上海","深圳"]
http://www.xxx.com
中华人民共和共国
["河北","河南","山西"]
中华人民共和共国
[11,22,33]
4.3 global关键字
默认情况下,在局部作用域对全局变量只能进行:读取和修改内部元素(可变类型),无法对全局变量进行重新赋值。
-
读取
COUNTRY = "中国" CITY_LIST = ["北京","上海","深圳"] def download(): url = "http://www.xxx.com" print(COUNTRY) print(CITY_LIST) download()
-
修改内部元素(可变类型)
COUNTRY = "中国" CITY_LIST = ["北京","上海","深圳"] def download(): url = "http://www.xxx.com" print(CITY_LIST) CITY_LIST.append("广州") CITY_LIST[0] = "南京" print(CITY_LIST) download()
-
无法对全局变量重新赋值
COUNTRY = "中国" CITY_LIST = ["北京","上海","深圳"] def download(): url = "http://www.xxx.com" # 不是对全部变量赋值,而是在局部作用域中又创建了一个局部变量 CITY_LIST 。 CITY_LIST = ["河北","河南","山西"] print(CITY_LIST) def upload(): file_name = "rose.zip" print(COUNTRY) print(CITY_LIST) download() upload()
如果想要在局部作用域中对全局变量重新赋值,则可以基于 global
关键字实现,例如:
COUNTRY = "中国"
CITY_LIST = ["北京","上海","深圳"]
def download():
url = "http://www.xxx.com"
global CITY_LIST
CITY_LIST = ["河北","河南","山西"]
print(CITY_LIST)
global COUNTRY
COUNTRY = "中华人民共和国"
print(COUNTRY)
def upload():
file_name = "rose.zip"
print(COUNTRY)
print(CITY_LIST)
download()
upload()
作业
-
如何查看一个值的内存地址?
v1 = 10 print(id(v1))
-
函数的参数传递的是引用(内存地址)还是值(拷贝一份)?
函数的参数传递的是引用 -
看代码写结果
v1 = {} v2 = v1 v1["k1"] = 123 print(v1,v2) # {'k1': 123} {'k1': 123}
-
看代码写结果
def func(k,v,info={}): info[k] = v return info v1 = func(1,2) print(v1) # {1:2} v2 = func(4,5,{}) print(v2) # {4:5} v3 = func(5,6) print(v3) # {1:2, 5:6}
-
看代码写结果
def func(k,v,info={}): info[k] = v return info v1 = func(1,2) v2 = func(4,5,{}) v3 = func(5,6) print(v1,v2,v3) # {1:2,5:6} {4:5} {1:2,5:6}
-
简述第4题、第5题的结果为何结果不同。
两题的打印时机不同 -
看代码写结果
def func(*args, **kwargs): print(args, kwargs) return "完毕" v1 = func(11, 22, 33) print(v1) # (11,22,33) {} v2 = func([11, 22, 33]) print(v2) # ([11,22,33],) {} v3 = func(*[11, 22, 33]) print(v3) # (11,22,33) {} v4 = func(k1=123, k2=456) print(v4) # () {"k1":123,"k2":456} v5 = func({"k1": 123, "k2": 456}) print(v5) # ({"k1":123,"k2":456},) {} v6 = func(**{"k1": 123, "k2": 456}) print(v6) # () {"k1":123,"k2":456} v7 = func([11, 22, 33], **{"k1": 123, "k2": 456}) print(v7) # ([11,22,33],) {"k1":123,"k2":456} v8 = func(*[11, 22, 33], **{"k1": 123, "k2": 456}) print(v8) # (11,22,33) {"k1":123,"k2":456}
-
看代码写结果
def func(*args,**kwargs): prev = "-".join(args) data_list = [] for k,v in kwargs.items(): item = "{}-{}".format(k,v) data_list.append(item) content = "*".join(data_list) return prev,content v1 = func("北京","上海",city="深圳",count=99) print(v1) # ('北京-上海', 'city-深圳*count-99') v2 = func(*["北京","上海"],**{"city":"深圳","count":99}) print(v2) # ('北京-上海', 'city-深圳*count-99')
-
补充代码,实现获取天气信息并按照指定格式写入到文件中。
import requests res = requests.get(url="http://www.weather.com.cn/data/ks/101010100.html") res.encoding = "utf-8" weather_dict = res.json() # 获取的天气信息是个字典类型,内容如下: print(weather_dict) """ { 'weatherinfo': { 'city': '北京', 'cityid': '101010100', 'temp': '18', 'WD': '东南风', 'WS': '1级', 'SD': '17%', 'WSE': '1', 'time': '17:05', 'isRadar': '1', 'Radar': 'JC_RADAR_AZ9010_JB', 'njd': '暂无实况', 'qy': '1011', 'rain': '0' } } """
import requests def write_file(**kwargs): """将天气信息拼接起来,并写入到文件 格式要求: 1. 每个城市的天气占一行 2. 每行的格式为:city-北京,cityid-101010100,temp-18... """ # 补充代码 def get_weather(code): """ 获取天气信息 """ url = "http://www.weather.com.cn/data/ks/{}.html".format(code) res = requests.get(url=url) res.encoding = "utf-8" weather_dict = res.json() return weather_dict city_list = [ {'code': "101020100", 'title': "上海"}, {'code': "101010100", 'title': "北京"}, ] # 补充代码
import requests def write_file(**kwargs): """将天气信息拼接起来,并写入到文件 格式要求: 1. 每个城市的天气占一行 2. 每行的格式为:city-北京,cityid-101010100,temp-18... """ # 补充代码 weather_info = kwargs['weatherinfo'] data_list = [] # 用于存储所有天气信息 for key, value in weather_info.items(): temp = "{}-{}".format(key, value) data_list.append(temp) line_info = ",".join(data_list) with open("weather.txt", mode="a", encoding="utf-8") as f: # 写入文件 f.write("{}\n".format(line_info)) def get_weather(code): """ 获取天气信息 """ url = "http://www.weather.com.cn/data/ks/{}.html".format(code) res = requests.get(url=url) res.encoding = "utf-8" weather_dict = res.json() return weather_dict city_list = [{'code': "101020100", 'title': "上海"}, {'code': "101010100", 'title': "北京"}, ] # 补充代码 for city in city_list: code = city['code'] weather = get_weather(code) write_file(**weather)
-
看代码写结果
def func():
return 1,2,3
val = func()
print( type(val) == tuple ) # True
print( type(val) == list ) # False
- 看代码写结果
def func(users,name):
users.append(name)
print(users)
result = func(['武沛齐','李杰'],'alex')
print(result) # None
- 看代码写结果
def func(v1):
return v1 * 2
def bar(arg):
return "%s 是什么玩意?" %(arg,)
val = func('你')
data = bar(val)
print(data) # 你你 是什么玩意?
- 看代码写结果
def func(v1):
return v1* 2
def bar(arg):
msg = "%s 是什么玩意?" %(arg,)
print(msg)
val = func('你')
data = bar(val)
print(data) # None
- 看代码写结果
def func():
data = 2 * 2
return data
data_list = [func,func,func]
for item in data_list:
v = item()
print(v) # 4
- 分析代码,写结果:
def func(handler,**kwargs):
extra = {
"code":123,
"name":"武沛齐"
}
kwargs.update(extra)
return handler(**kwargs)
def something(**kwargs):
return len(kwargs)
def killer(**kwargs):
key_list = []
for key in kwargs.keys():
key_list.append(key)
return key_list
v1 = func(something,k1=123,k2=456)
print(v1) # 4
v2 = func(killer,**{"name":"武沛齐","age":18})
print(v2) # ['name', 'age', 'code']
- 两个结果输出的分别是什么?并简述其原因。
def func():
return 123
v1 = [func,func,func,func,]
print(v1) # [<function func at 0x0000022C6D141D30>, <function func at 0x0000022C6D141D30>, <function func at 0x0000022C6D141D30>, <function func at 0x0000022C6D141D30>]
v2 = [func(),func(),func(),func()]
print(v2) # [123,123,123,123]
- 看代码结果
v1 = '武沛齐'
def func():
print(v1)
func() # 打印"武沛齐"
func() # 打印"武沛齐"
- 看代码结果
v1 = '武沛齐'
def func():
print(v1)
func() # 打印"武沛齐"
v1 = '老男人'
func() # 打印"老男人"
- 看代码写结果
NUM_LIST = []
SIZE = 18
def f1():
NUM_LIST.append(8)
SIZE = 19
def f2():
print(NUM_LIST)
print(SIZE)
f2() # [] 18
f1()
f2() # [8] 18
- 看代码写结果
NUM_LIST = []
SIZE = 18
def f1():
global NUM_LIST
global SIZE
NUM_LIST.append(8)
SIZE = 19
def f2():
print(NUM_LIST)
print(SIZE)
f2() # [] 18
f1()
f2() # [8] 19
- 根据要求实现资源下载器。
-
启动后,让用户选择专区,每个专区用单独的函数实现,提供的专区如下:
- 下载 花瓣网图片专区
- 下载 抖音短视频专区
- 下载 NBA锦集 专区
-
在用户选择了某个功能之后,表示进入某下载专区,在里面循环提示用户可以下载的内容选项(已下载过的则不再提示下载)
提醒:可基于全部变量保存已下载过得资源。 -
在某个专区中,如果用户输入(Q/q)表示 退出上一级,即:选择专区。
-
在选择专区如果输入Q/q则退出整个程序。
-
每个专区实现下载的案例如下:
-
图片
# 可供用户下载的图片如下 image_dict = { "1":("吉他男神","https://hbimg.huabanimg.com/51d46dc32abe7ac7f83b94c67bb88cacc46869954f478-aP4Q3V"), "2":("漫画美女","https://hbimg.huabanimg.com/703fdb063bdc37b11033ef794f9b3a7adfa01fd21a6d1-wTFbnO"), "3":("游戏地图","https://hbimg.huabanimg.com/b438d8c61ed2abf50ca94e00f257ca7a223e3b364b471-xrzoQd"), "4":("alex媳妇","https://hbimg.huabanimg.com/4edba1ed6a71797f52355aa1de5af961b85bf824cb71-px1nZz"), }
# 下载图片示例 import request res = requests.get( url="https://hbimg.huabanimg.com/4edba1ed6a71797f52355aa1de5af961b85bf824cb71-px1nZz", headers={ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" } ) with open("alex媳妇.png",mode="wb") as f: f.write(res.content)
-
短视频
# 可供用户下载的短视频如下 video_dict = { "1":{"title":"东北F4模仿秀",'url':"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f570000bvbmace0gvch7lo53oog"}, "2":{"title":"卡特扣篮",'url':"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f3e0000bv52fpn5t6p007e34q1g"}, "3":{"title":"罗斯mvp",'url':"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f240000buuer5aa4tij4gv6ajqg"}, }
# 下载视频示例 import requests res = requests.get( url="https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f240000buuer5aa4tij4gv6ajqg", headers={ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS" } ) with open('罗斯mvp.mp4', mode='wb') as f: f.write(res.content)
-
NBA
# 可供用户下载的NBA视频如下 nba_dict = { "1":{"title":"威少奇才首秀三双","url":"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300fc20000bvi413nedtlt5abaa8tg&ratio=720p&line=0"}, "2":{"title":"塔图姆三分准绝杀","url":"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0d00fb60000bvi0ba63vni5gqts0uag&ratio=720p&line=0"} }
# 下载示例 import requests res = requests.get( url="https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0d00fb60000bvi0ba63vni5gqts0uag&ratio=720p&line=0", headers={ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS" } ) with open('塔图姆三分准绝杀.mp4', mode='wb') as f: f.write(res.content)
-
完整代码如下:
import requests
SELECTED_IMAGE_SET = set() # 存放已下载的id
SELECTED_VIDEO_SET = set() # 存放已下载的id
SELECTED_NBA_SET = set() # 存放已下载的id
def download(file_path, url):
resp = requests.get(url, headers={"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS"})
with open(file_path, mode="wb") as f:
f.write(resp.content)
def download_image():
total_image_dict = {
"1": ("吉他男神", "https://hbimg.huabanimg.com/51d46dc32abe7ac7f83b94c67bb88cacc46869954f478-aP4Q3V"),
"2": ("漫画美女", "https://hbimg.huabanimg.com/703fdb063bdc37b11033ef794f9b3a7adfa01fd21a6d1-wTFbnO"),
"3": ("游戏地图", "https://hbimg.huabanimg.com/b438d8c61ed2abf50ca94e00f257ca7a223e3b364b471-xrzoQd"),
"4": ("alex媳妇", "https://hbimg.huabanimg.com/4edba1ed6a71797f52355aa1de5af961b85bf824cb71-px1nZz"),
}
while True:
text_list = []
for num, item in total_image_dict.items():
if num in SELECTED_IMAGE_SET: # 不让重复下载
continue
data = "{}-{}".format(num, item[0])
text_list.append(data)
if text_list:
text = ";".join(text_list)
else:
text = "无可下载选项"
print(text)
index = input("请输入要选择的序号:")
if index.upper() == 'Q':
return
if index in SELECTED_IMAGE_SET:
print("已下载,无法再继续下载,请重新选择")
continue
group = total_image_dict.get(index)
if not group:
print("序号不存在,请重新选择")
continue
# 下载图片
file_path = "{}.png".format(group[0])
download(file_path, group[1])
# 将已下载的id加入集合
SELECTED_IMAGE_SET.add(index)
def download_video():
total_video_dict = {
"1": {"title": "东北F4模仿秀", 'url':
"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f570000bvbmace0gvch7lo53oog"},
"2": {"title": "卡特扣篮", 'url':
"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f3e0000bv52fpn5t6p007e34q1g"},
"3": {"title": "罗斯mvp", 'url':
"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f240000buuer5aa4tij4gv6ajqg"}, }
while True:
text_list = []
for num, item in total_video_dict.items():
if num in SELECTED_VIDEO_SET: # 不让重复下载
continue
data = "{}-{}".format(num, item['title'])
text_list.append(data)
if text_list:
text = ";".join(text_list)
else:
text = "无可下载选项"
print(text)
index = input("请输入要选择的序号:")
if index.upper() == 'Q':
return
if index in SELECTED_VIDEO_SET:
print("已下载,无法再继续下载,请重新选择")
continue
group = total_video_dict.get(index)
if not group:
print("序号不存在,请重新选择")
continue
# 下载图片
file_path = "{}.mp4".format(group['title'])
download(file_path, group['url'])
# 将已下载的id加入集合
SELECTED_VIDEO_SET.add(index)
def download_nba():
total_nba_dict = {
"1": {"title": "威少奇才首秀三双",
"url": "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300fc20000bvi413nedtlt5abaa8tg&ratio=720p&line=0"},
"2": {"title": "塔图姆三分准绝杀",
"url": "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0d00fb60000bvi0ba63vni5gqts0uag&ratio=720p&line=0"}}
while True:
text_list = []
for num, item in total_nba_dict.items():
if num in SELECTED_NBA_SET: # 不让重复下载
continue
data = "{}-{}".format(num, item['title'])
text_list.append(data)
if text_list:
text = ";".join(text_list)
else:
text = "无可下载选项"
print(text)
index = input("请输入要选择的序号:")
if index.upper() == 'Q':
return
if index in SELECTED_NBA_SET:
print("已下载,无法再继续下载,请重新选择")
continue
group = total_nba_dict.get(index)
if not group:
print("序号不存在,请重新选择")
continue
# 下载图片
file_path = "{}.mp4".format(group['title'])
download(file_path, group['url'])
# 将已下载的id加入集合
SELECTED_NBA_SET.add(index)
print("欢迎使用xxx系统")
func_dict = {
"1": download_image,
"2": download_video,
"3": download_nba,
}
while True:
print("1.花瓣网图片专区 2.抖音短视频专区 3.NBA锦集 专区")
choice = input("请选择序号:")
if choice.upper() == 'Q':
break
func = func_dict.get(choice)
if not func:
print("输入有误,请重新选择")
continue
# 进入专区
func()