I'm learning Python and stumbled upon the OOP part. At the time I did my first steps with the PHP OOP but the same thing happens to me, I don't understand how to use the self value inside a method.
I've been reading around the internet and it talks about self-referencing the class and so on, but honestly, I don't understand that concept.
For example:
class Persona:
def inicializar(self,nom):
self.nombre=nom
def imprimir(self):
print("Nombre",self.nombre)
persona1=Persona()
persona1.inicializar("Pedro")
persona1.imprimir()
persona2=Persona()
persona2.inicializar("Carla")
persona2.imprimir()
Would someone be so kind as to explain to me what this is for self
?
In Python almost everything is an object, unlike other languages, a class is an object as well, it sounds weird but it really is like this:
We can see that the class
Prueba
is an object of type<class 'type'>
and the instantiated object is now if, of type<class 'Prueba'>
. With this in mind it may be clearer why the use of theself
. Suppose something similar to your example:We have used the object
Persona
of type<class 'type'>
(class) to create two new objects of<class 'Persona'>
. The normal way to access methods would be to do this:But what is actually happening underneath, that is, what Python is actually doing, is something like this:
One way to see this is that each instance
p1
and does not havep2
"copies" of the methodimprimir
does not make sense, the method belongs to the classPersona
, the dataself.nombre
does belong to each instance. So in order for it to bePersona
able to be invokedimprimir
but with the proper instance data, you need to declare the method asimprimir(<instancia>)
.Clarification:
self
it is a convention we could use any name but it is not recommended.To complete and to take into account. The use of
self
is a convention in Python, a good practice but not a rule (it's not a reserved word or anything like that).By this I mean that you can use any name to represent the class instance:
For Python this is a perfectly normal class. In the class
A
I have changed theself
toinstancia
. Note , this is just a clarification.You may also come across some methods that lack
self
such as when using static methods:The use of
self
as the first argument of methods is specified in Function and method arguments of PEP 8 (the style guide for Python code).The parameter
self
refers to the instantiated object of that class on which said method is being invoked. That is, the object you used to call the method (in your examplepersona1
andpersona2
).Python, within the defined methods of a class, establishes that the first parameter defined in a method will receive the object with which said method is invoked. This parameter (which is usually called
self
although any variable name can be used [ see comments ]) is used within the method implementation to modify the content or attributes of said object as desired.Therefore, it is a necessary condition that all methods of a class that can be called through an object have at least one parameter, which will be automatically assigned to the object used in the invocation.
Although in the definition of the method,
self
it is the first parameter, when calling said method it is not necessary to explicitly pass the object itself as the first parameter, since Python does it implicitly without having to do it manually. That is, the first parameterself
of the method is automatically assigned to the object itself and the rest of the parameters to the arguments with which you call the method.In the case of the method
__init__()
, the parameterself
refers to the newly instantiated object of the class that you want to get when creating said object withNombre_Clase()
.self
is the equivalentthis
of other languages (althoughself
it is not a reserved word likethis
[ see comments ]), with the difference that in other languages it is not necessary to define the methods with a parameterthis
, while in Python it is necessary.As you say, it is a self reference and in general, in a model instance method, you should use self when modifying attributes or when there is some ambiguity between calling a local variable and an already defined attribute/method. This happens in all languages that use OOP and it is a very common practice and the most correct, so I advise you to use it.