【Python】常见语法逻辑错误收集

每次1. list

问题: 某地方参数需要传入一个list

当时采用的方法为:

1 phone_list = []
2 send_message(phone_list.append(get_phone_numbers(authorization_code)),....)
3 
4 # phone_list = []
5 # phone_list.append(get_phone_numbers(authorization_code))
6 # send_message(phone_list,........)

1-2 行的方式 获取的是append这个函数的返回值。但是这个函数返回值为None

5-6 行获取的才是list真正的值

 

——————————————————————————————————————————————————————————————————

 2.list.append 数据覆盖问题

问题:每次输出字典的内容是不一样的,但是append入list的时候前面数据都会被覆盖

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 所有值都一样的问题

 

 

 ——————————————————————————————————————————————————————————————————

posted @ 2018-10-28 23:53  AbyssViper  阅读(301)  评论(0编辑  收藏  举报