A default value makes a function parameter optional: Python uses the specified fallback when the caller omits that argument. Parameters without defaults must come first, and extra arguments still raise an error. section 7.1
Use a default when the function has a known optional value:
- def hello(name='world'): return f'Hello, {name}!' hello() # 'Hello, world!' hello('out there') # 'Hello, out there!' section 7.1
- Put required parameters before defaulted ones. Prefer a default over *args when the function accepts one known optional value rather than an arbitrary number of values. section 7.1 section 7.3
- Never use a mutable list or dict as a default: default objects are stored and reused, so mutations can leak between calls. section 7.1
Optional XML content
def myxml(tagname, content='', **kwargs): attrs = ''.join([f' {key}="{value}"' for key, value in kwargs.items()]) return f'<{tagname}{attrs}>{content}</{tagname}>' The required tagname is always supplied; content defaults to an empty string, while **kwargs handles separately named attributes. section 7.3



















