Closed MartinThoma closed 2 years ago
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
body=[
Assign(
targets=[Name(id='name', ctx=Store())],
value=Constant(value='some_default', kind=None),
type_comment=None,
),
If(
test=Compare(
left=Constant(value='some_key', kind=None),
ops=[In()],
comparators=[Name(id='some_dict', ctx=Load())],
),
body=[
Assign(
targets=[Name(id='name', ctx=Store())],
value=Subscript(
value=Name(id='some_dict', ctx=Load()),
slice=Constant(value='some_key', kind=None),
ctx=Load(),
),
type_comment=None,
),
],
orelse=[],
),
],
type_ignores=[],
)
Related:
if "key" in some_dict:
for item in some_dict["key"]:
False-Positive (solved):
if key in some_dict:
file = some_dict[key]
else:
... # anything
False-positive:
if key in some_dict:
# other_dict["other_key"] does not exist otherwise!
other_dict["other_key"] = some_dict['key']
I've also seen:
name = "some_default" if "some_key" not in some_dict else some_dict["some_key"]
Kind of related, but might suit a separate rule:
# bad
if key in some_dict:
some_dict[key].append(value)
else:
some_dict[key] = [value]
# good
from collections import defaultdict
some_dict = defaultdict(list)
some_dict[key].append(value)
+ sets, dicts as values
I've used defaultdict in the past, but in the past 1-2 years I've rather used this pattern:
# bad
if key in some_dict:
some_dict[key].append(value)
else:
some_dict[key] = [value]
# Good
if key not in some_dict:
some_dict[key] = []
some_dict[key].append(value)
I think it's typically clearer than a default dictionary, especially if the dictionary is passed to other functions.
Explanation
The
get(key, default_fallback=None)
method of a dictionary is often overlooked. It provides a clear interface that every Python developer should know.Example