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.
remove-unit-step-from-range
Replace range(x, y, 1)
with range(x, y)
for i in range(y, len(x), 1):
do_t()
for i in range(y, len(x)):
do_t()
The default step
value for a call to range()
is 1
, so it is unnecessary to
explicitly define it. This refactoring removes this argument, slightly
shortening the code.