W0102

Message

Dangerous default value %s as argument

Description

Used when a mutable value as list or dictionary is detected in a default value for an argument.

Explanation

This is the message that inspired the creation of the PyLint Messages wiki in the first place!!

Ned Batchelder gives a good, bad example:

def my_method(self, arg1, extras=[]):
    // blah blah...

To a Python novice, this may seem like a reasonable way to default to an empty array for the extras param. What really happens is that this "default" array gets created as a persistent object, and every invocation of my_method that doesn't specify an extras param will be using that same list object—any changes to it will persist and be carried to every other invocation!

See Ned Batchelder's blog post of 14 June 2008 about PyLint, which highlights this problem and suggests some better alternatives.


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License
{"cc_re__cc_re__cc_by_sa_3_0": ["t:;\" id=\"bug-report-button\" onclick=\"WIKIDOT.page.listeners.pageBugReport(event)\">Report a bug</a>\n    &nbsp;|\n    <a href=\"javascript:;\" id=\"abuse-report-button\" onclick=\"WIKIDOT.page.listeners.flagPageObjectionable(event)\">Flag as objectionable</a>\n</div>\nPowered by <a href=\"http://www.wikidot.com\">Wikidot.com</a> \n            </div>\n            \n                <div id=\"license-area\" class=\"license-area\">\n                    Unless otherwise stated, the content of this page is licensed under <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/\">Creative Commons Attribution-ShareAlike 3.0 License</a>\n                </div>\n            \n            \n            \n\n\n\n            <div id=\"extrac-div-1\"><span></span></div><div id=\"extrac-div-2\"><span></span></div><div id=\"extrac-div-3\"><span></span></div>\n            \n            \n            \n            \n                <div id=\"footer-bar\">\n  <div>\n     </div>\n  <h2>Other interesting sites</h2>\n  \n  <div class=\"units\">\n              <div class=\"unit\">\n          <div class=\"inner\">\n       "]}