Python Frequently Asked Questions, Release 3.9.5 It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined. If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object. By definition, immutable objects such as numbers, strings, tuples, and None , are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion. Because of this feature, it is good programming practice to not use mutable objects as default values. In- stead, use None as the default value and inside the function, check if the parameter is None and create a new list/dictionary/whatever if it is. For example, don’t write: This feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called “memoizing”, and can be implemented like this: You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste. Collect the arguments using the$_{*}$and$_{**}$ specifiers in the function’s parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using$_{*}$and$_{**}$ : def f(x,*args,**kwargs): ... kwargs['width'] ='14.3c ' ... g(x,*args,**kwargs) # Callers can only provide two parameters and optionally pass _cache by keyword def expensive(arg1, arg2, *, _cache ={}): if (arg1, arg2) in _cache: return _cache[(arg1, arg2)] # Calculate the value result=...expensive computation... _cache[(arg1, arg2)] =result # Store result in the cache return result def foo(mydict= None ): if mydict is None : mydict ={} # create a new dict for local namespace def foo(mydict={}): ... but: 2.2.7 How can I pass optional or keyword parameters from one function to an- other? Chapter 2. Programming FAQ 12