Reputation: 71
I am reading a book about Object-Oriented Programming in Python. There is a sentence that I am confused by:
The interpreter automatically binds the instance upon which the method is invoked to the
self
parameter.
In this sentence what is bound to the instance. the method, or the self
parameter?
Upvotes: 2
Views: 151
Reputation: 53535
This is actually not such a bad question and I'm not sure why it got downvoted so quickly...
Even though Python supports object-oriented, I find it to be much closer to functional-programming languages, one of the reasons for that is that functions are invoked "on" objects, not "by" them.
For example: len(obj)
where in a "true" object oriented programing language you'd expect to be able to do something like obj.length()
In regards to the self
parameter, you're calling obj.method(other_args)
but what really happens under the hood is a translation of this call to: method(obj, other_args)
you can see that when the method is declared you're doing it with the self
variable passed in as the first argument:
class ...
def method(self, other_args):
...
so it's basically all about the "translation" of obj.method(other_args)
to method(obj, other_args)
Upvotes: 2