Skip to content

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.

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

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

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.