Skip to content

Commit dc5a8fa

Browse files
author
ting.wang
committed
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 (like list and dict) 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 Change-Id: If187f16bfb305ac4fe6e4177e498a06c49c3f946
1 parent ab6ba38 commit dc5a8fa

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

openstackclient/common/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def get_field(item, field):
163163
raise exceptions.CommandError(msg)
164164

165165

166-
def get_item_properties(item, fields, mixed_case_fields=[], formatters={}):
166+
def get_item_properties(item, fields, mixed_case_fields=None, formatters=None):
167167
"""Return a tuple containing the item properties.
168168
169169
:param item: a single item resource (e.g. Server, Project, etc)
@@ -172,6 +172,11 @@ def get_item_properties(item, fields, mixed_case_fields=[], formatters={}):
172172
:param formatters: dictionary mapping field names to callables
173173
to format the values
174174
"""
175+
if mixed_case_fields is None:
176+
mixed_case_fields = []
177+
if formatters is None:
178+
formatters = {}
179+
175180
row = []
176181

177182
for field in fields:
@@ -187,7 +192,7 @@ def get_item_properties(item, fields, mixed_case_fields=[], formatters={}):
187192
return tuple(row)
188193

189194

190-
def get_dict_properties(item, fields, mixed_case_fields=[], formatters={}):
195+
def get_dict_properties(item, fields, mixed_case_fields=None, formatters=None):
191196
"""Return a tuple containing the item properties.
192197
193198
:param item: a single dict resource
@@ -196,6 +201,11 @@ def get_dict_properties(item, fields, mixed_case_fields=[], formatters={}):
196201
:param formatters: dictionary mapping field names to callables
197202
to format the values
198203
"""
204+
if mixed_case_fields is None:
205+
mixed_case_fields = []
206+
if formatters is None:
207+
formatters = {}
208+
199209
row = []
200210

201211
for field in fields:

0 commit comments

Comments
 (0)