Avoid Builtin Shadow
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery comment id: avoid-builtin-shadow
Section titled “Sourcery comment id: avoid-builtin-shadow”Description
Section titled “Description”Don’t assign to builtin variables, such as list.
Before
Section titled “Before”list = [1, 1, 2, 3, 5, 8]Possible Fix
Section titled “Possible Fix”fibonacci = [1, 1, 2, 3, 5, 8]Explanation
Section titled “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.