[Python-ideas] Fwd: Detecting Attribute Updates on Objects

Gustavo Rehermann gugurehermann at gmail.com
Fri Feb 17 13:43:13 EST 2017


Whoops, I forgot to subscribe before. Seems like now I'm able to send this
idea, so let's try again :)

================

According to this idea, the interpreter calls a function on an object O
everytime an attribute A of it changes, recursing through objects that
contain O as attribute as well. Avoiding loops too (refer to P3 below).

Explanation:
P1. When setting any property of an object inside a property, the
interpreter should also call "__update_attr__(self, attr_name, old_item,
new_item)" in the object that contains it. This automatically calls the
same thing on every object that contains this one too, until a loop is hit
(see P3), which makes it possible for a perfect dict synchronization with a
file or stuff like that. This should also work for lists and any kind of
object that contains attributes; so for every object O that contains A as
attribute, whenever updating any of A's attributse call __update_attr__()
or something on O.

P2. From here on, refer as parents to any objects which contain the current
object as attribute.

P3. Python internally stores a cache - a tree of references - to speed up
the process. if __update_attr__ finds a loop then it stops at the top
container and instead sends it a call __update_loop__(). Whenever either or
both __update_attr__ or __update_loop__() functions are overwritten by a
non-function, the interpreter skips that call in the tree (see P5).

P4. To save on performance, the interpreter first checks the
__detect_update__ attribute of the class (it is a single-byte int/ctypes
char, and is used as a bit mask): if bit 1 is OFF, then proceed the calls;
otherwise, skip this and go to the parents in the tree (or don't call
__update_loop__ at all if the call was about an update loop); if bit 2 is
ON, then don't iterate on the parent; otherwise proceed to every parents as
normal.

P5. Bit 3 is only accessible from an interpreter (any attempt to change it
will have the interpreter change it back to what it was before), and when
ON forces the interpreter to skip this object or stop iterating, depending
on Bit 2. This bit has precedence over Bit 1 and is ON always if
__update_attr__ is a non-function. Other bits are ignored.

P6. By default __detect_update__ is b10 on both bytes, or b00 if the
function was overwritten by another function.

Thank you,
Gustavo Ramos "Gustavo6046" Rehermann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170217/1b8b90e8/attachment-0001.html>


More information about the Python-ideas mailing list