Skip to content

Commit

Permalink
Enable @observe decorator by making ObserveHandler inherit from BaseD…
Browse files Browse the repository at this point in the history
…escriptor
  • Loading branch information
SylvainCorlay committed Aug 4, 2015
1 parent ac3be19 commit 1b9a0b3
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,28 @@ class dict to the newly created class ``cls``.
super(MetaHasTraits, cls).__init__(name, bases, classdict)


def observe(*names):
return ObserveHandler(names)


class ObserveHandler(BaseDescriptor):

def __init__(self, names=None):
if names is None:
self.names=[None]
else:
self.names = names

def __call__(self, func):
self.func = func
return self

def instance_init(self, inst):
setattr(inst, self.name, self.func)
for name in self.names:
inst.observe(self.func, name=name)


class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):
"""The base class for all classes that have traitlets.
"""
Expand Down

0 comments on commit 1b9a0b3

Please sign in to comment.