Atom provides specially named methods that the framework calls automatically. These are not intended to be called directly by user code.
Post-setattr methods
To perform an action immediately after a member's value is set, define a method following the pattern _post_setattr_<member_name>(self, old, new). This method receives the previous value (old) and the newly assigned value (new).
Observer methods
There are two ways to observe changes to a member:
- Mangled method name: Define a method named
_observe_<member_name>(self, change). This method receives a dictionary containing information about the modification. @observe decorator: Decorate a method with @observe('<member_name>').
Warning: For container members like List, the observer is only triggered when the container itself is replaced. Changes to the container's contents (e.g., via .append()) will not trigger the observer.
from atom.api import Atom, Int, List, observe
class CompactObject(Atom):
int_value = Int(10)
list_value = List()
# Called after int_value is set
def _post_setattr_int_value(self, old, new):
print(f'Changed from {old} to {new}')
# Called when int_value changes
def _observe_int_value(self, change):
print(change)
# Alternative observer using decorator
@observe('list_value')
def notify_change(self, change):
print(change)