Skip to content

Instantly share code, notes, and snippets.

@yusuke0519
Last active September 16, 2022 12:29
Show Gist options
  • Save yusuke0519/3e97b1dbbdb530a03d0bc6ca7feaa4a7 to your computer and use it in GitHub Desktop.
Save yusuke0519/3e97b1dbbdb530a03d0bc6ca7feaa4a7 to your computer and use it in GitHub Desktop.
再帰的なsetattr(nameが"."を含む場合のsetattr)
def recursive_setattr(obj, name, value):
if '.' in name:
parent, child = name.split('.')[0], '.'.join(name.split('.')[1:])
recursive_setattr(getattr(obj, parent), child, value)
else:
setattr(obj, name, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment