I am reading the official python documentation on python classes here: https://docs.python.org/release/3.8.5/tutorial/classes.html?highlight=class.
In section 9.3.2, following an example of class definition
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
there is a remark:
So in our example,
x.f
is a valid method reference, sinceMyClass.f
is a function, butx.i
is not, sinceMyClass.i
is not. Butx.f
is not the same thing asMyClass.f
— it is a method object, not a function object.
I am a bit confused about the difference between a method object and a function object. To me they are both some object that can be called. After more reading, I think the difference is:
- A function object must receive a full list of argument(s) as in its definition.
- A method object receives some arguments implicitly, for example, the
x.f
method above, when called, receives the instancex
implicitly as its first argumentself
.
I have some questions:
- Is this the correct understanding of the difference between a method object and function object?
- Is this the sole difference between them?
- Are there any other, maybe more complicated, examples that illustrates the difference?