Skip to content

Avoid Builtin Shadow

Sourcery comment id: avoid-builtin-shadow

Description

Don't assign to builtin variables, such as list.

Before

list = [1, 1, 2, 3, 5, 8]

Possible Fix

fibonacci = [1, 1, 2, 3, 5, 8]

Explanation

Python has a number of builtin variables: functions and constants that form a part of the language, such as list, getattr, and type (See https://docs.python.org/3/library/functions.html). It is valid, in the language, to re-bind such variables:

list = [1, 2, 3]

However, this is considered poor practice.

  • It will confuse other developers.
  • It will confuse syntax highlighters and linters.
  • It means you can no longer use that builtin for its original purpose.

How can you solve this?

Rename the variable something more specific, such as integers. In a pinch, my_list and similar names are colloquially-recognized placeholders.

Back to top