Fix Mutable default argument

Python’s default arguments are evaluated once when
the function is defined, not each time the function is called.

This means that if you use a mutable default argument and
mutate it, you will and have mutated that object for all
future calls to the function as well.

more details about this wrong usage here:
http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments
Closes-Bug: #1534974

Change-Id: Ia87abe0d020d7840dbf5f8a3063f057bab92c2ba
This commit is contained in:
binean
2016-01-24 23:40:14 +08:00
parent 747ff27405
commit b188b1372b
2 changed files with 8 additions and 2 deletions

View File

@@ -21,7 +21,11 @@ places where actual behavior differs from the spec.
from __future__ import print_function
def assert_has_keys(dictonary, required=[], optional=[]):
def assert_has_keys(dictonary, required=None, optional=None):
if required is None:
required = []
if optional is None:
optional = []
for k in required:
try:
assert k in dictonary

View File

@@ -800,7 +800,9 @@ class ShellTest(test_utils.TestCase):
def test_extract_metadata(self):
# mimic the result of argparse's parse_args() method
class Arguments:
def __init__(self, metadata=[]):
def __init__(self, metadata=None):
if metadata is None:
metadata = []
self.metadata = metadata
inputs = [