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())
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With