Referrers, Referents, Parents and Children

2019-08-02 20:29发布

I'm experimenting with the Dill package, specifically it's detect module and having some trouble intuitively understanding what's is meant by referents, referers, parents and children.

A reference is a value that enables access to some data.

And referents are objects that are referred to, right?

So in the following code:

class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'

an_instance = MyClass()
an_instance2 = MyClass()
an_instance3 = MyClass()

a_list = [an_instance, an_instance2, an_instance3]

Are an_instance, an_instance2, an_instance3 referents of a_list and would the MyClass also be a referent of a_list, but one level of depth further up the chain?

So, with -> signifying the reference, would the chain of referents look like:

a_list -> an_instance -> MyClass

Would this be viewed as:

grandchild -> child -> Parent

Conversely, is a_list a referrer of an_instance as well as an_instance2, an_instance3 and at another level of depth, MyClass?

Making the chain of referrers:

MyClass -> an_instance -> a_list

And would this also be conceived:

parent -> child -> grandchild

Can someone offer a clear explanation of where references, inheritance and containers do and don't coincide?

1条回答
何必那么认真
2楼-- · 2019-08-02 21:27

In python, inheritance builds a pointer relationship between the class object and the class instance object. For example, a class instance first checks it's own __dict__ then points back to it's class definition to find any missing attribute. Similarly, instance methods can be seen as partials that are applied over class methods, again giving a pointer relationship to the underlying class method. With python, inheritance is little more than when an object can't find some attribute within itself it looks back to the parent (in the mro) for the missing attribute -- that hierarchy is built through a pointer relationship.

That's about the extent that inheritance and pointer references are the same. Pointer references are much more general.

查看更多
登录 后发表回答