MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
186 stars 19 forks source link

[New Rule] Use set_default in Python dictionaries #182

Open Skylion007 opened 1 year ago

Skylion007 commented 1 year ago

Explanation

Explain briefly why you think this makes the code simpler.

Example

# Bad
a = {"key": "value"}
if "key" not in a:
    a["key"] = "default_value"

# Good
a = {"key": "value"}
a.set_default("key", "default_value")

Faster, more efficient, removes an if statement.