In Python, is there any rule or advantage regarding the use of 'Self'?

Asked

Viewed 1,172 times

8

Let us consider the examples below:

Example 1:

> class PrintText():
>     def __init__(self):
>         text = 'Funciona!'
>         self.printa(text)
>         
>     def printa(self, text):
>         print(text)

Example 2:

> class PrintText():
>     def __init__(self):
>         self.text = 'Funciona!'
>         self.printa()
>         
>     def printa(self):
>         print(self.text)

Let’s assume that in the Printtext class there are several cases like this, where a certain variable is used by only one or two functions; Is it better to use Example 1 that passes the variable as argument to the function, or use Example 2 that explicitly declares the 'self'? The two examples work, but there is an advantage between one or the other, or one of them is incorrect?

2 answers

7


So - the two forms work - what is done in the first example is that, even when it comes to calling a class method, the parameter is explicitly passed - that is - the "printa" function could be a normal Python function, not a method of the same class, who would play the same role.

In the second way, the value of "text" is annotated as an object attribute - and the method is called without any explicit parameter - it uses the object’s "text" attribute.

In practice, the first approach is typical of a structured application design. Even using the class, and objects, the "type of reasoning" that is used when parameters are explicitly passed is the structured.

In contrast, grouping data and functions that will work with this data (the methods) into the same object, is the very foundation of "Object-Oriented" thinking. One of the great gains of object-oriented programming is precisely not having to pass all the parameters that represent the state of that object as parameters for the methods: the methods have access to these values in the form of attributes.

In your example you are using a single attribute. But a complex object could have dozens of distinct attributes - a character in a game, for example, has to have as parameters the coordinates on the map, a reference to the map object itself, how much energy, speed, if he is in possession of some item (and which)and, to be effectively drawn on the screen, still have to have reference to the images themselves that will be used to draw it, position on the screen, and so on - using the structured approach, these dozens of parameters would have to be passed in several function calls to handle the same object.

Note that if someone is using a data structure that groups the parameters related to an item - this game character, for example, and passes this structure as a parameter for all functions that will deal with the character, is happening the opposite of what happens in your first example: using a structured programming in the form, but in practice is object oriented.

That’s the conceptual part. Speaking of more technical, and Python-specific details - in your first example, the value of "text" is accessible with that name in the method __init__ as a local variable, and again as a local variable in the function printa. In the second example, the local variable that is used in the two methods is the self - the value of text is stored in an "object variable" - i.e., inside the dictionary __dict__ of the object self. Run a test, and have it printed self.__dict__["text"]. In this way it is visible and accessible to anyone who has access to the object - in the concept of OO, it is a "public" attribute. In the first form, no function outside the object, nor other methods of the object, can access the value of self.

  • Very well answered, thank you! But I still have one question: Disregarding situations where we do not want to make the variable accessible to 'everyone', is there any situation where the use of the 'self' can be considered a bad practice, there is indiscriminate use of the self?

  • 1

    If you are with an object-oriented design and using classes in your program, better use the self. However Python allows an application to be drawn in a structured or functional way - hence one does not use the self anyway. The "way of thinking" in the problems with the two approaches changes.

2

Hello. Both examples are correct but it is more common to use the self of example 2. Example of self use:

class Animal(object):
is_alive = True
def __init__(self, name, age):
    self.name = name
    self.age = age
# Add your method here!
def description(self):
    print self.name
    print self.age

I hope I helped :D

  • Could you clarify for me the idea I wanted to pass off as 'vulgar'? I did not understand if I should take it literally or if I meant vulgar in the sense of 'most used''.

  • In the sense of most used.

  • 2

    @Henry just one note: I suggest that when putting snippets use them in Portuguese. Not that I believe I’m wrong, but I think it’s easier for site users to understand esta_vivo to is_alive for example.

  • @Kyllopardiun, thanks for the tip :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.