numpy/array-default-argument
| Rule | array-default-argument |
|---|---|
| Pack | NumPy |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
A NumPy array as a default argument is one array that every call shares, so a write to it shows up in later calls. Default to None and build the array inside the function.
def add_one(acc=np.zeros(2)):def add_one(acc=None): acc = np.zeros(2) if acc is None else acc acc += 1 return accWhat the rule reports
Section titled “What the rule reports”The rule reports def f(acc=np.zeros(2)) when the function writes into acc, because every call shares the one default array.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
The default for $P is one array that every call shares, and this function writes into it, so later calls see the change. Default to None and build the array inside the function.Browse the other rules of the NumPy pack.