12 votes

What is self used for in POO in Python?

I'm learning Python and I came across the part of POO. In its time I made my first foray into OOP PHP but I the same thing happens, I do not understand that it will use the value of self inside a method.

I've been reading online and speaking of autoreferenciar to the class and others, but honestly, I don't understand that concept.

For example:

class Persona:

    def inicializar(self,nom):

    def imprimir(self):



Does anyone would be so kind as to explain to me what it is that self?


César Points 14430

To complete and to take into account. The use of self is a convention in Python, a good practice but not a rule (it is not a reserved word or anything like that).

With this I mean that you can use any name to represent the instance of the class:

>>> class A:
...   def __init__(instancia, numero):
...     instancia.numero = numero
...   def get_numero(instancia):
...     return instancia.numero
>>> a = A(10)
>>> a.get_numero()

For Python this is a class that is perfectly normal. In the class A I have changed the self for instancia. Eye, this is just a clarification.

You can also find some methods that lack self such as when using static methods:

>>> class A:
...   def __init__(self, numero):
...     self.numero = numero
...   @staticmethod
...   def saludo():
...     print('Hola')
>>> a = A(10)
>>> a.saludo()

The use of self as the first argument of the methods is specified in Function and method arguments of the PEP 8 (style guide for Python code).


Patricio Moracho Points 24098

In Python almost everything is an object, unlike other languages, a class is an object too, sounds weird but it is effectively so:

class Prueba():

p = Prueba()


> <class 'type'>
> <class 'Prueba'>

We can see that the class Prueba it is a object of type <class 'type'> and the instantiated object is now if, of the type <class 'Prueba'>. With this in mind it may be more clear the because of the use of self. Suppose something similar to your example:

class Persona:

    def __init__(self,nom):

    def imprimir(self):
        print("Nombre: {0}".format(self.nombre))

p1 = Persona("Juan")
p2 = Persona("Pedro")

> <class 'type'>
> <class 'Persona'>
> <class 'Persona'>

We have used the item Persona type <class 'type'> (class) to create two new objects <class 'Persona'>. The normal form of access to methods would be doing this:

> Juan
> Pedro

But what is actually happening underneath, that is what Python actually does, is something like this:

> Juan
> Pedro

One way to view this is that each instance p1 and p2 do not own "copies" the method imprimir don't have sense, the method is of the class Personadata self.nombre if any, are each instance. So for that Persona may invoke imprimir but with the data of the appropriate instance, it is necessary to declare the method as a imprimir(<instancia>).

Explanation: self is a convention we could use any name but it is not recommended.


Carlos A. Gómez Points 1302

The parameter self refers to the object instantiated of that class on which you're invoking that method. That is to say, the object used to call the method (in your example persona1 and persona2).

Python, within the defined methods of a class, sets the first parameter defined on a method will receive the object which invoked the method. This parameter (which is usually called self while you can use any variable name [see comments]) is used within the implementation of the method to modify the content or attributes of that object as you wish.

Therefore, it is a necessary condition that all the methods of a class that can be called through an object having at least one parameter, which is automatically assigned to the object used in the invocation.

Although the definition of the method, self is the first parameter, when calling the method don't need to pass the object itself as the first parameter explicitly because Python does it implicitly without the need of doing it manually. That is to say, the first parameter self 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 parameter self refers to the newly instantiated object of the class that you want to get when you create this object with Nombre_Clase().

self is equivalent to this of other languages (although self is not a reserved word, such as this [see comments]), with the difference that in other languages it is necessary to define methods with a parameter this, while that in Python is necessary.


Manuel Robles Points 1168

As you say, it is a self reference and in general, in an instance method of a model, 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 the languages that use POO and it is a very common practice and the most correct, so I advise you to use it.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: