Skip to content

Remove None From Default Get

Sourcery refactoring id: remove-none-from-default-get


Replace dict.get(x, None) with dict.get(x)


hats = {"bowler": Bowler(), "sombrero": Sombrero()}
fedora = hats.get("fedora", None)


hats = {"bowler": Bowler(), "sombrero": Sombrero()}
fedora = hats.get("fedora")


When using a dictionary's get method you can specify a default to return if the key is not found. This defaults to None, so it is unnecessary to specify None if this is the required behaviour. Removing the unnecessary argument makes the code slightly shorter and clearer.

Back to top