What is the "self" for? [duplicate]

3

I'm studying Python to complement my programs on Nodejs.

But why in Python, a language that values speed in development and ease in code, is the self used?

Very simple self-use example

class ButtonPerson(Button, Person):
    def __init__(self, name):
        Person.__init__(self)
        Button.__init__(self, "Pessoa: " + name)
        self.set_name(name)

Is it redundant?

    
asked by anonymous 30.08.2017 / 17:54

2 answers

2

Although in another language, this question was answered in STO itself . So I just made one adptation:

The reason you need to use the self is because Python does not use @ syntax to refer to instance attributes.

The Python team decided to make methods in a way that causes the instance to which the method belongs to be passed automatically but not received automatically: The first parameter of a method is the instance where the method is called. / p>

This causes the methods to be entirely equal to the functions, and leaves the actual name to be used, at the user / developer's charge (although the word self is convention and most of people will twist their nose if you use any other word).

  

Edited
  Example avoiding self (do not twist your nose!) :-)
  Note: I ran in a (local) python 3 terminal, in the repl.it was not accepted, it requires self

class Foo():
    def __init__(this, msg):     
        this.msg = msg

    @property    
    def bar(this): 
        return 'Hello '+this.msg

foo = Foo('bar')
foo.bar
'Hello bar'

self There's nothing special about the code, it's just another object.

Python developers could have opted for another strategy to distinguish "normal names" from attributes - Special syntax like Ruby, or require statements like C ++ and Java, or perhaps something else - SQN. / p>

Python has as one of the pillars of its philosophy the determination to do things as explicitly as possible, although of course this goal can not always be achieved.

As a consequence of this goal, you need self , to assign to an instance attribute, explicit to which instance to assign, self informs you that the assignment is the yourself.

    
30.08.2017 / 19:26
1

A comparison that may help you understand, is to think of self as if it were this of java / C #. That is not explicit in the implementation but also points to the object itself.

In the case of python, when you call a method of a class it automatically passes this parameter in the first position.

The first parameter of a class receives an instance of the same object and is used to access attributes and methods of the same object.

I would like to make it clear that there is no obligation to do so we can use any other name for the parameter.

  

It is often called the first self parameter because most   Python programmers already recognize this name as the name of the object to   be invoked in the method; in addition, this is the default specified by   PEP-8. For this reason, as a rule it is better to use self as the name   of the first parameter of the methods.

    
30.08.2017 / 19:05