2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
While the assignment of default parameter values is typically a good thing, it can go very wrong very quickly when mutable objects are used. That's because a new instance of the object _is not_ created for each function invocation. Instead, all invocations share the same instance, and the changes made for one caller are made for all!
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
def get_attr_array(obj, arr=[]): # Noncompliant
props = (name for name in dir(obj) if not name.startswith('_'))
arr.extend(props) # after only a few calls, this is a big array!
return arr
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
def get_attr_array(obj, arr):
props = (name for name in dir(obj) if not name.startswith('_'))
arr.extend(props)
return arr
----
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove the default value of "xxx".
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]