What’s the "self" for?

Asked

Viewed 1,404 times

4

I am studying Python to complement my programs in Nodejs.

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

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 not redundant?

  • 1
  • @bfavaretto found this philosophy cool, but still do not see the pq of self...rs

  • To better understand the question: how would you write something with this same structure in JS, since you said you use Node?

2 answers

3


Although in another language, this question was answered in STO itself. So I just did an assignment:

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

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

This makes the methods entirely equal to the functions, and leaves the real name to be used, in charge of the user/developer (although the word self be the convention and most people will turn their nose if you use any other word).

Edited
Example avoiding the self (don’t twist your nose!) :-)
Obs. I circled in a python 3 (local) terminal, no repl it. was not accepted, demands the 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 distigure "normal names" from attributes -- Special syntax like Ruby, or require statements like C++ and Java or perhaps anything else-- SQN.

Python has as one of the pillars of his philosophy the determination to do things as explicitly as possible, although obviously this goal may not always be achieved.

As a consequence of this goal, Voce needs the self, to make an assignment to an instance attribute, it is necessary to spell out to which instance to assign, self informs that the assignment is itself.

1

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

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

The first parameter of a class, takes an instance of that same object and serves 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 parameter of self because most Python programmers already recognize this name as the name of the object to be invoked in the method; moreover, this is the pattern specified by PEP-8. Therefore, as a rule it is better to use self as the name of the first method parameter.

Browser other questions tagged

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