Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification on a paragraph in the Python Tutorial Documentation

I'm unclear as to what this one paragraph in the python tutorial documentation is saying.

(found here: https://docs.python.org/3/tutorial/classes.html#method-objects)

When an instance attribute is referenced that isn’t a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object. When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list.

From my current understanding, I think what it's saying is that whenever you reference an attribute of an instance of a class like in the 8th line of this little snippet here:

class MyClass():
    attribute = "I am an attribute"

    def func(self):
        return "I am a function"

instance = MyClass()
print(instance.func())

When python sees

instance.func()

what it's really doing isn't looking for a method func "owned by" instance, it's looking for a function func owned by MyClass, then calling that function owned by MyClass with instance as the self parameter.

so basically it's the same thing as:

MyClass.func(instance)

I feel like I'm missing something subtle though. I don't understand what it means by

... a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object.

What is an abstract object?

What does it mean to "pack" a pointer?

What does it mean to "pack" multiple pointers?

Why even have a method object for instance if python is just going to look at MyClass's function object?

Why doesn't python just make methods be "owned by" their instances? Why even go through the whole process of calling MyClass's func instead of instance's func?

Why did the designers of the language decide to make it be this way?

like image 480
vaughn-mcgill-adami Avatar asked Nov 07 '22 05:11

vaughn-mcgill-adami


1 Answers

"Abstract object" means that there isn't necessarily a real Python object being created, it's just a way of describing what's happening behind the scenes as if there were some object being created.

"packing" means that it's just collecting these things together into this abstract object.

So when you write

instance.func()

it internally creates something that represents the function combined with the instance. When this is called, the method function is called as you described, with the instance passed as the first argument (conventionally named self).

Why do this? So that you can pass these things around:

foo = instance.func
foo()

The value of foo contains that abstract object that represents the function combined with the instance.

Methods are owned by classes so that all instances of a class automatically get the same method. This is the essence of OO programming and the basis of inheritance among classes.

like image 158
Barmar Avatar answered Nov 13 '22 06:11

Barmar