diff options
Diffstat (limited to 'networkx/classes/graph.py')
| -rw-r--r-- | networkx/classes/graph.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/networkx/classes/graph.py b/networkx/classes/graph.py index ebbc8b53..47d5f81d 100644 --- a/networkx/classes/graph.py +++ b/networkx/classes/graph.py @@ -41,6 +41,28 @@ class _CachedPropertyResetterAdj: del od["adj"] +class _CachedPropertyResetterNode: + """Data Descriptor class for _node that resets ``nodes`` cached_property when needed + + This assumes that the ``cached_property`` ``G.node`` should be reset whenever + ``G._node`` is set to a new value. + + This object sits on a class and ensures that any instance of that + class clears its cached property "nodes" whenever the underlying + instance attribute "_node" is set to a new object. It only affects + the set process of the obj._adj attribute. All get/del operations + act as they normally would. + + For info on Data Descriptors see: https://docs.python.org/3/howto/descriptor.html + """ + + def __set__(self, obj, value): + od = obj.__dict__ + od["_node"] = value + if "nodes" in od: + del od["nodes"] + + class Graph: """ Base class for undirected graphs. @@ -282,6 +304,7 @@ class Graph: """ _adj = _CachedPropertyResetterAdj() + _node = _CachedPropertyResetterNode() node_dict_factory = dict node_attr_dict_factory = dict |
