2022年4月27日 星期三

python argument empty array error


Finally: 

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


 There is a technique called memoization 


https://nikos7am.com/posts/mutable-default-arguments/


Avoid using an empty list as a default argument to a function


A very common error in Python is the use of an empty list as a default argument to a function. This is not the right way to do it and can cause unwanted behavior. See for example below:

def append_to_list(element, list_to_append=[]):
    list_to_append.append(element)
    return list_to_append
>>> a = append_to_list(10)
[10]
>>> b = append_to_list(20)
[10, 20]

沒有留言:

張貼留言