SCR107: mutable-default-argument
Mutable value as a parameter default
| Code | SCR107 |
| Name | mutable-default-argument |
| Category | Scripts (SCR) |
| Default severity | warning |
| Enabled by default | yes |
| Fix available | no |
| ign-lint equivalent | none |
What it does
Section titled “What it does”Reports parameter defaults that are mutable objects created once: list,
dict and set literals and comprehensions, and calls to the builtins
list(), dict(), set() and bytearray().
Why is this bad?
Section titled “Why is this bad?”The default is evaluated once, when the def runs. Every call that does
not pass the argument shares the same object, so changes made by one call
are seen by the next. Library modules live for the whole gateway uptime:
such a list keeps growing across every Perspective session.
Example
Section titled “Example”def addAlarm(alarm, seen=[]): seen.append(alarm) return len(seen)Use instead:
def addAlarm(alarm, seen=None): if seen is None: seen = [] seen.append(alarm) return len(seen)pylint
Section titled “pylint”Corresponds to W0102 dangerous-default-value.
Generated from the rule metadata by iglint generate-docs; iglint rule SCR107 prints the same page in the terminal. Source: docs/rules/SCR107.md.