I have some confusion about the method implementation in Python. Any help in that regard would be appreciated.
I found this closely-related post, which is quite useful, but not answering my questions.
My problem: The following statements from the last paragraph of Section 9.3.4 from Python documentation are not quite clear to me.
When a non-data attribute of an instance is referenced, the instance’s 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.
My initial understanding from the doccumentation was very similar to that of @vaughn-mcgill-adami as described in their post: when a class instance object calls its method object, a search would be done on the class object for the corresponding function object. If found, then calling that function owned by the class object with the class instance object as the first parameter.
However, after some coding, it seems to me that my understanding may not be correct. The reason is that one could delete the class object. Yet, calling the method object can still be done with no problem, whereas passing the class instance object to the function object from the class object would result in an error, since there would be no class object anymore after its deletion.
The following code snippet aims at better explaining my questions:
class myclass:
x=0
def inc(self):
self.x +=1
print(self.x)
myobj = myclass()
type(myobj.inc) #output: method
type(myclass.inc) #output: function
# 1. calling method object multiple times
# 2. calling function object and passing the instance object multiple times
print(myobj.x)
for i in range(3):
myobj.inc()
for i in range(3):
myclass.inc(myobj)
#-------------
# output:
#-------------
# 0
# 1
# 2
# 3
# 4
# 5
# 6
#-------------
# deleting the class object
del myclass
# calling method object multiple times
for i in range(3):
myobj.inc()
#-------------
# output:
#-------------
# 7
# 8
# 9
#-------------
# calling function object and passing the instance object
for i in range(3):
myclass.inc(myobj)
#-------------
# output:
#-------------
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-177-86f094e96103> in <module>
1 for i in range(3):
----> 2 myclass.inc(myobj)
NameError: name 'myclass' is not defined
My understanding from the attached code snippet is that after instantiating a class object, the method objects are attached to the class instance object. The method objects and the class instance object will then be independent of the class object.
Here are some ambiguities I have from the documentation:
What does it mean "...the instance’s class is searched"? I believe it should be "instance class object", but it does not seem to be a typo, since in the following sentence it says "If the name denotes a valid class attribute that is a function object...". Again, here I think the "method object" should replace the "function object".
"...a method object is created...". Is the method object created once it is called, or at the same time that the class instance object is created?
"by packing (pointers to) the instance object and the function object just found". Again, "function object" does not seem correct to me.
Basically, if the class object is deleted, as in my code snippet, then there is no class object to be searched on in the first place. There will be no function object to be pointed to either.
There is a good chance that I am missing something here. I would be grateful if anyone could please advise. Thank you