I'm learning the Python programming language and I've came across something I don't fully understand.
In a method like:
def method(self, blah):
def __init__(?):
....
....
What does self
do? What is it meant to be? Is it mandatory?
What does the __init__
method do? Why is it necessary? (etc.)
I think they might be OOP constructs, but I don't know very much.
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self.In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
Python doesn't force you on using "self". You can give it any name you want. But remember that the first argument in a method definition is a reference to the object.Python adds the self argument to the list for you; you do not need to include it when you call the methods. if you didn't provide self in init method then you will get an error
init short for initialization. It is a constructor which get called when you make instance of the class and it is not necessary. But usually it our practice to write init method for setting default state of the object. If you are not willing to set any state of the object initially then you don't need to write this method.
In short:
self
as it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, thenself.a
will refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variablea
for each object__init__
is what is called as a constructor in other OOP languages such as C++/Java. The basic idea is that it is a special method which is automatically called when an object of that Class is createdIn this code:
... the
self
variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of theA
class and call its methods, it will be passed automatically, as in ...The
__init__
method is roughly what represents a constructor in Python. When you callA()
Python creates an object for you, and passes it as the first parameter to the__init__
method. Any additional parameters (e.g.,A(24, 'Hello')
) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.The example given is not correct, so let me create a correct example based on it:
When we create an instance of the object, the
__init__
is called to customize the object after it has been created. That is, when we callSomeObject
with'blah'
below (which could be anything), it gets passed to the__init__
function as the argument,blah
:The
self
argument is the instance ofSomeObject
that will be assigned toan_object
.Later, we might want to call a method on this object:
Doing the dotted lookup, that is,
an_object.method
, binds the instance to an instance of the function, and the method (as called above) is now a "bound" method - which means we do not need to explicitly pass the instance to the method call.The method call gets the instance because it was bound on the dotted lookup, and when called, then executes whatever code it was programmed to perform.
The implicitly passed
self
argument is calledself
by convention. We could use any other legal Python name, but you will likely get tarred and feathered by other Python programmers if you change it to something else.__init__
is a special method, documented in the Python datamodel documentation. It is called immediately after the instance is created (usually via__new__
- although__new__
is not required unless you are subclassing an immutable datatype).The 'self' is a reference to the class instance
Now we can create an instance of foo and call the method on it, the self parameter is added by Python in this case:
But it can be passed in as well if the method call isn't in the context of an instance of the class, the code below does the same thing
Interestingly the variable name 'self' is just a convention. The below definition will work exactly the same.. Having said that it is very strong convention which should be followed always, but it does say something about flexible nature of the language
Here, the guy has written pretty well and simple: https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
Read above link as a reference to this: