字典键初始化
如果你不确定密钥是否存在,请首选 dict.get 方法。如果找不到密钥,它允许你返回默认值。传统方法 dict[key] 会引发一个 KeyError 异常。
而不是做
def add_student():
try:
students['count'] += 1
except KeyError:
students['count'] = 1
做
def add_student():
students['count'] = students.get('count', 0) + 1