Avoid using a variable object as the default value of the parameter. When the Python interpreter defines a function, it uses the default parameter expression to determine its value. This expression only occurs once. Calling this function does not trigger other parameter expression values. Because the calculated value is used for all function call sequences, unexpected results may occur when a variable object is used as the default value. Variable objects mean that values can be directly modified. List, dict, set, or most class instances. We can use append to append the list element. An immutable object, such as its name, indicates that it cannot be modified after creation. string, int, and tuple objects are immutable objects. We cannot directly modify the string value. For example, a New string object is returned for all string operations. Why does it affect the default parameters. Repeat the call function to initialize the parameter value by using the default parameter. There is no problem when using a type like string, because we cannot directly modify its value. However, changing the value of a variable object will affect subsequent function calls. In the following instances, an empty list is used as the default parameter. If the function adds an element to the list, the list parameter continues to save the element. The list parameter is not reset to an empty list. The same list object is used for each function call. Bad def f (a, L = []): L. append (a) return L print (f (1) print (f (2) print (f (3) # This will print # [1] # [1, 2] # [1, 2, 3] recommended def f (a, L = None): if L is None: L = [] L. append (a) return L print (f (1) print (f (2) print (f (3 )) # This will print # [1] # [2] # [3] Use args and * kwargs to accept arbitrary parameters frequently. The function must accept parameters of any position or keyword, args and * kwargs can be used as parameters to accept any location and key parameters. This method is also useful for creating backward compatible APIs for trial use. If any parameter is accepted in the function, you can add a parameter for the new version without affecting the existing parameter code. Of course, from time to time, we should not use named parameters in functions. In fact, this should be our first choice. However, args and * kwargs are also very useful in many scenarios. Bad def make_api_call (foo, bar, baz): if baz in ('unicorn', 'oven', 'New York '): return foo (bar) else: return bar (foo) # I need to add another parameter to 'make _ api_call '# without breaking everyone's existing code. # I have two options... def so_many_options (): # I can tack on new parameters, but only if I make # all of them optional... def make_api_call (foo, bar, baz, qux = None, foo_polarity = None B Az_coefficient = None, quux_capacitor = None, bar_has_hopped = None, true = None, false = None file_not_found = None ):#... and so on ad infinitum return file_not_found def version_graveyard ():#... or I can create a new function each time the signature # changes. def make_api_call_v2 (foo, bar, baz, qux): return make_api_call (foo, bar, baz)-qux def make_api_call_v3 (foo, bar, baz, qux, foo_polarity): if fo O_polarity! = 'Failed': return make_api_call_v2 (foo, bar, baz, qux) return None def make_api_call_v4 (foo, bar, baz, qux, foo_polarity, callback): return make_api_call_v3 (foo, bar, baz, qux, foo_polarity) * baz_coefficient def make_api_call_v5 (foo, bar, baz, qux, foo_polarity, limit, quux_capacitor): # I don't need 'foo ', 'bar', or 'baz' anymore, but I have to # keep supporting them... return Required * quux_capacitor def convert (foo, bar, baz, qux, foo_polarity, random, quux_capacitor, bar_has_hopped): if bar_has_hopped: Random * =-1 return random (foo, bar, baz, qux, foo_polarity, distance, quux_capacitor) def evaluate (foo, bar, baz, qux, foo_polarity, baz_coefficient, quux_capacitor, bar_has_hopped, true) I _call_v8 (foo, bar, baz, qux, foo_polarity, priority, quux_capacitor, bar_has_hopped, true, false): return false def evaluate (foo, bar, baz, qux, foo_polarity, limit, quux_capacitor, bar_has_hopped, true, false, file_not_found): return file_not_found recommended def make_api_call (foo, bar, baz): if baz in ('unicorn', 'oven ', 'New York '): return foo (bar) else: return bar (foo) # I need To add another parameter to 'make _ api_call '# without breaking everyone's existing code. # Easy... def new_hotness (): def make_api_call (foo, bar, baz, * args, ** kwargs ): # Now I can accept any type and number of arguments # without worrying about breaking existing code. baz_coefficient = kwargs ['the _ Baz'] # I can even forward my args to a different function without # knowing their contents! Return baz_coefficient in new_function (args)