Python 常見語法邏輯錯誤收集
阿新 • • 發佈:2020-12-13
每次1. list
問題: 某地方引數需要傳入一個list
當時採用的方法為:
phone_list = []
send_message(phone_list.append(get_phone_numbers(authorization_code)),....)
# phone_list = []
# phone_list.append(get_phone_numbers(authorization_code))
# send_message(phone_list,........)
1-2 行的方式 獲取的是append這個函式的返回值。但是這個函式返回值為None
5-6 行獲取的才是list真正的值
2.list.append 資料覆蓋問題
問題:每次輸出字典的內容是不一樣的,但是append入list的時候前面資料都會被覆蓋
'''
遇到問題沒人解答?小編建立了一個Python學習交流QQ群:778463939
尋找有志同道合的小夥伴,互幫互助,群裡還有不錯的視訊學習教程和PDF電子書!
'''
def get_init_list():
insert_list = []
temp_path_dict = {}
for dir_path, dir_names, file_names in os.walk(base_config[ 'root_path']):
省略部分
if not white_flag:
# temp_path_dict = {} 應該被插入的部分
temp_path_dict['filePath'] = full_path
temp_path_dict['fileMd5'] = get_file_md5(full_path)
print(id(temp_path_dict))
insert_list. append(temp_path_dict)
return insert_list
可以看出,字典的部分是定義在迴圈外面的,雖然裡面字典的值變了,但是賦值的都是相同的位置
如果在應該插入部分,每次dict的空間都是申請的新的空間,所以不會出現最後list 所有值都一樣的問題