14. Accessing a dictionary value with a default value

Use the dictionary .get method with a supplied default value.

14.1. Don’t do this

1d = {
2    'username': 'jdoe'
3}
4
5is_authorized = False
6if 'auth_token' in d:
7    is_authorized = True
1d = {
2    'username': 'jdoe'
3}
4
5is_authorized = True if 'auth_token' in d else False

14.2. Do this

1d = {
2    'username': 'jdoe'
3}
4
5is_authorized = d.get('auth_token', False)