<jupyter_start><jupyter_text>Default Values - Beware!<jupyter_code>from datetime import datetime
print(datetime.utcnow())
def log(msg, *, dt=datetime.utcnow()):
    print(f'{dt}: {msg}')
log('message 1')
log('message 2', dt='2001-01-01 00:00:00')
log('message 3')
log('message 4')<jupyter_output>2022-02-14 04:55:48.911897: message 4<jupyter_text>As you can see, the default for **dt** is calculated when the function is **defined** and is **NOT** re-evaluated when the function is called.  Solution Pattern Here is one pattern we can use to achieve the desired result:We actually set the default to None - this makes the argument optional, and we can then test for None **inside** the function and default to the current time if it is None.<jupyter_code>def log(msg, *, dt=None):
    dt = dt or datetime.utcnow()
    # above is equivalent to:
    #if not dt:
    #    dt = datetime.utcnow()
    print('{0}: {1}'.format(dt, msg))    
dt = None
dt = dt or datetime.utcnow()
print(dt)
bool(1) == bool(False)
None == False
log('message 1')
log('message 2')
log('message 3', dt='2001-01-01 00:00:00')
log('message 4')<jupyter_output>2022-02-14 04:58:43.803934: message 4<jupyter_text>Another Example<jupyter_code>def add_pet_to_list(pet, pets=[]):
    pets.append(pet)
    return pets

list_with_cat = add_pet_to_list("cat") 
print(list_with_cat) 
list_with_dog = add_pet_to_list("dog") 
print(list_with_dog)<jupyter_output>['cat', 'dog']<jupyter_text>Remember that although we can execute a function body many times, a function definition is executed only once – that means that the empty list which is created in this function definition will be the same list for all instances of the function. What we really want to do in this case is to create an empty list inside the function body:<jupyter_code>def add_pet_to_list(pet, pets=None):
    if pets is None:
        pets = [] # pets = list()
    pets.append(pet)
    return pets

list_with_cat = add_pet_to_list("cat")
list_with_dog = add_pet_to_list("dog")

print(list_with_cat)
print(list_with_dog) # oops<jupyter_output>['cat']
['dog']
{'fraction_non_alphanumeric': 0.12320230105465005, 'fraction_numerical': 0.03883029721955897, 'mean_word_length': 5.1382352941176475, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '329999', 'license': 'MIT', 'n_tokens_mistral': 768, 'n_tokens_neox': 715, 'n_words': 229, 'path': 'Functional Programming/Function Parameters/09 - Parameter Defaults - Beware.ipynb', 'repo_name': 'Rafiei-tayebe/AP4002', 'size': '2086'}