Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty dict as default value of dict.get()

I am attempting to pass an empty dictionary as the default return value of a call to .get()on another dictionary.

queryStringParameters = event.get('queryStringParameters', ({})
size = queryStringParameters.get('size', 25)

However, queryStringParameters.get(...) throws an error saying that queryStringParameters is None.

'NoneType' object has no attribute 'get': AttributeError
Traceback (most recent call last):
File "/var/task/api/game.py", line 13, in all
size = queryStringParameters.get('size', 25)
AttributeError: 'NoneType' object has no attribute 'get'

Is this not possible, or am I doing something wrong? I have tried the following as well:

queryStringParameters = event.get('queryStringParameters', dict())
like image 337
Groppe Avatar asked Dec 23 '22 09:12

Groppe


1 Answers

Chances are that event actually contains a key queryStringParameters that happens to be None. Did you check if the key exists?

The default value is only returned if the key does not exists, not if it exists and happens to be None.

But you can also write

queryStringParameters = event.get('queryStringParameters', {}) or {}

to be on the safe side.

like image 165
Mike Scotty Avatar answered Jan 28 '23 12:01

Mike Scotty