No need to check for existance of a key in kwargs before hand ?

I think I’m trying to convert from PHP to Python

How does this work automatically ?

if kwargs.get('type') == 'book':

We don’t seem to have to do

if kwargs.get('type') is not None:
    if kwargs.get('type') == 'book':
        query = Q(type='book')

In PHP we have to do like

if (isset($args['type']))
{
    if ($args['type'] == 'book')
    {

    }
}

I’m not sure I understand the root question here.

See the docs for the get method of a dict.

None is never going to equal ‘book’ if ‘type’ is not a key within kwargs.

I don’t need to seem to check the existence of a key before checking if a particular key’s value is something.

In PHP, if ($args['type'] == 'book') will throw an error if $args['type'] doesn’t exist.

Checking for existence of a key in dict is a coding practice and not a python requirement.

Your code still works without the outer if block, and if the mapping (dictionary) key is not found, a KeyError will be raised. You need to use try except block, or check for key existence.

Ok I’m missing something - right now in my views.py :

if kwargs.get('type') == 'book':

is not raising any an exception if { 'type' : 'anything' } doesn’t exist.

We generally use the following code to get the value associated with a specific key in the Python dictionary.

value = dict[key]

But, trying to get a value for a key that doesn’t exist will throw an exception.

However, the kwargs.get('somestring') will not raise an exception; and you are passed a standard Python dict type.