Open jayvdb opened 5 years ago
@jayvdb Thanks for this issue.
If the core types need to be subclassed in order to be observed, a helper/mixin would be useful to make it easy.
We'd probably want to observe different methods on different built-in types. For example, on Dict
we probably want to observe __setitem__
while on Set
we probably want to observe add
. So I'm not sure there's a useful mixin that can be written here. I could imagine that offering an example observable Dict could be useful, but then again that's really only a few lines of code using the standard library's collections
module.
What do you think?
For example:
class MyDict(collections.abc.MutableMapping):
def __init__(self, *args, **kw):
self.__dict__.update(*args, **kw)
def __getitem__(self, key):
return self.__dict__[key]
@observed.observable_method()
def __setitem__(self, key, val):
self.__dict__[key] = val
def __delitem__(self, key):
del self.__dict__[key]
def __iter__(self):
return iter(self.__dict__)
def __len__(self):
return len(self.__dict__)
Mixins for dicts, sets and lists would be useful especially so that there are tests for it, in addition to docs. While there are only a few of each in the stdlib collections module, including frozen variants, there are many other derived subtypes, so implementer should be able to easily mix a custom dict, e.g. from sortedcontainers, with a ObservableDictMixin, and not have to worry about all the dunders in your example above, and expect it will keep working until at least python 4 with the libraries dealing with any breakages that occur along the way.
Im following this ... lurking ... but this discussion makes me think a PEP that requests hooks into various data structure mutation points would be a great idea. That way we could avoid subclassing at all.
Any ideas of that has ever been proposed?
Not sure if there is a PEP about mutations. Sounds useful. irrelevant extra comment about subclasses.
@jayvdb Would you like to add ObservableDictMixin
? Pull requests are always welcome.
I would like to have observable dicts. This seems like a pattern that observed might want to provide an implementation for, and docs with a recipe for observing other core collection types. If the core types need to be subclassed in order to be observed, a helper/mixin would be useful to make it easy.