Working further with the Dill package. Posted yesterday exemplifying some ignorance of referents, referrers, inheritance and containers. The answer was insightful, but I'm still having trouble coming up with examples that show a few levels of depth in referrer and referent chains.
Yesterday I was thinking that an instance
would be a referrer
to it's class. In the Dill docs children are the referrers, so in that case a child of depth=2
would be a grandchild, right? And would that be an object
that refers (points) to another object
that refers to another object
?
What would be an example of an object that has a chain of referents and referrers of at least a depth of two?
Consider:
import dill
class GreatGrandparentClass(object):
"""A Great Grandparent class"""
name = "Henrietta Ancient One"
class GrandparentClass(GreatGrandparentClass):
"""A Grandparent class"""
class ParentClass(GrandparentClass):
"""A Grandparent class"""
great_grand_parent = ParentClass().name
print ("Children (depth=2):")
for element in dill.detect.children(
great_grand_parent,
list,
depth=2,
ignore=(globals())):
print(element)
print ("Parents:")
for element in dill.detect.parents(
great_grand_parent,
list,
depth=2,
ignore=(globals())):
print(element)
returns:
Children (depth=2):
['\npython pydill.py\n\n', 'dill', 'object', 'A Great Grandparent class', 'i', 'Henrietta Ancient One', 'GreatGrandparentClass', 'GreatGrandparentClass', 'A Grandparent class', 'GrandparentClass', 'GrandparentClass', 'A Grandparent class', 'ParentClass', 'great_grand_parent', 'ParentClass', 'i', 'Children (depth=2):', 'element', 'dill', 'detect', 'children', 'great_grand_parent', 'list', 'depth', 2, 'ignore', 'globals', 'element', 'Parents:', 'element', 'dill', 'detect', 'parents', 'great_grand_parent', 'list', 'depth', 2, 'ignore', 'globals', 'element']
Henrietta Ancient One
Parents:
Henrietta Ancient One
Looking specifically at list
objects here, the single referrent (Parent) of great_grand_parent
is the string, "Henrietta Ancient One".
And the referrers (Children) (result of gc.get_referrers()
, filtered by specified object-type) contains two objects: A list
that includes the string 'Henrietta Ancient One', and the string
Henrietta Ancient One. (depth=2 and depth=1 return the same result.)
How can I make an object for which Dill can return:
- Two distinct depths of referrers
- Two distinct depths of referents