Skip to content

Instantly share code, notes, and snippets.

@leveled
Created February 22, 2021 00:46
Show Gist options
  • Save leveled/ad354747be88eb248dd7ecbac7519c58 to your computer and use it in GitHub Desktop.
Save leveled/ad354747be88eb248dd7ecbac7519c58 to your computer and use it in GitHub Desktop.
Capture all keyword arguments provided to a function in Python
def tag(tag_name, **attributes):
attribute_list = [
f'{name}="{value}"'
for name, value in attributes.items()
]
return f"<{tag_name} {' '.join(attribute_list)}>"
>>> tag('a', href="http://treyhunner.com")
'<a href="http://treyhunner.com">'
>>> tag('img', height=20, width=40, src="face.jpg")
'<img height="20" width="40" src="face.jpg">'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment