1、查看值的内存地址
a = id('wa')
print(a)
2、函数的参数传递的是引用(内存地址)还是值(拷贝一份)
3、看代码写结果
v1 = {}
v2 = v1
v1['k1'] = 123
print(v1,v2)
4、看代码写结果
def func(k,v,info={}): 5,6,{1,2}
info[k] = v
return info
v1 = func(1,2)
print(v1)
v2 = func(3,4,{})
print(v2)
v3 = func(5,6)
print(v3)
5、补充代码,实现获取天气并写入文件
import requests
def write_file(**kwargs):
data_list = []
row_dict = kwargs['weatherinfo']
for k, v in row_dict.items():
group = '{}-{}'.format(k, v)
data_list.append(group)
row_string = ','.join(data_list)
with open('weather.txt', mode='a', encoding='utf-8') as file_object:
file_object.write('{}\n'.format(row_string))
def get_weather(code):
proxies = {
"http": "http://{}:{}@代理服务器域名/".format('',''),
"https": "https://{}:{}@代理服务器域名/".format('','')
}
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 item in city_list:
result_dict = get_weather(item['code'])
write_file(**result_dict)