[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnulib-tool.py: Don't use mutable default arguments.
From: |
Collin Funk |
Subject: |
gnulib-tool.py: Don't use mutable default arguments. |
Date: |
Sat, 13 Apr 2024 19:03:17 -0700 |
User-agent: |
Mozilla Thunderbird |
This patch fixes an interesting warning given by PyCharm.
With this line:
def __init__(self, config: GLConfig, transformers: dict[str,
tuple[re.Pattern, str] | None] = dict()) -> None:
Under '= dict()', I see a warning about mutable default arguments.
Here is a test program to demonstrate:
--------------------------------
#!/usr/bin/env python3
def function(arg1, arg2 = dict()):
arg2[arg1] = 0
print(arg2)
function('one')
function('two')
function('three')
--------------------------------
When executing the following is printed:
{'one': 0}
{'one': 0, 'two': 0}
{'one': 0, 'two': 0, 'three': 0}
To avoid this behavior we can set the default value of 'transformers'
to None. Then in the body of __init__() we can set it to an empty
dictionary if it is None.
Collin
0001-gnulib-tool.py-Don-t-use-mutable-default-arguments.patch
Description: Text Data
- gnulib-tool.py: Don't use mutable default arguments.,
Collin Funk <=