Closed Bug 575414 Opened 15 years ago Closed 15 years ago

don't use mutable arguments to functions

Categories

(Testing :: Mozbase, defect)

x86
Linux
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: k0scist, Unassigned)

Details

In general, mutable arguments (chiefly [] and {}) should not be used as default arguments to functions. The defaults are stored on the function object and changing the object will change them for all instances: {{{ > cat tmp.py class Foo(object): def __init__(self, env={}): print env self.env = env self.env[len(self.env)] = 'foo' Foo() Foo() > python tmp.py {} {0: 'foo'} }}} The places where mutable arguments live should be changed to None; then, if the passed argument is None then you can assign the value as desired: {{{ > cat tmp.py class Foo(object): def __init__(self, env=None): print env self.env = env or {} self.env[len(self.env)] = 'foo' Foo() Foo() > python tmp.py {} {0: 'foo'} }}}
I've been fixing this incrementally. Everyone else should too. Can we close this bug since its not really about a specific piece of code? Additional cases where mutable arguments are used as function defaults should either be fixed in situ or ticketed separately.
I'm going to close this bug. I've corrected in various commits the associated errors I've found here. IMHO, it is not worth a full audit to find all the places where this should be done, and instead, these sorts of things should be corrected in situ. Feel free to reopen if you disagree.
Status: NEW → RESOLVED
Closed: 15 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.