Skip to content

Instantly share code, notes, and snippets.

@thmsklngr
Created September 13, 2022 09:51
Show Gist options
  • Save thmsklngr/e1e136bf070d8c9da580f5ccd79f1bdb to your computer and use it in GitHub Desktop.
Save thmsklngr/e1e136bf070d8c9da580f5ccd79f1bdb to your computer and use it in GitHub Desktop.
python-attrs - setting other attributes when one attribute is set
def _set_y(instance, attribute, value):
object.__setattr_(instance, 'y', value)
# returning a value is important, otherwise attribute will become None
return value
@define
class MyClass:
x: str = field(on_settattr=[_set_y])
y: str = field()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment