1

I know that some of you will think It's a stupid question but I will ask anyway.

why do we need to pass 'self' on all class methods can't we use it without passing it like this:

class Player:
    def __init__(name):
        self.name = name 
    
    def print_player_name():
        print(self.name)
Othman9095
  • 47
  • 6
  • 2
    I believe it's just how python works. I could imagine that `my_player.print_player_name()` actually calls `Player.print_player_name(my_player)` – Einliterflasche Oct 28 '21 at 17:13
  • its like passing a object to function just that `self` represent the class itself. – Papop Lekhapanyaporn Oct 28 '21 at 17:16
  • Also be aware the methods in your example are more properly called "instance methods". The term "class method" is a bit different and traditionally use `cls` instead of `self`. They are proceeded by the `@classmethod` decorator and apply to the whole class rather than a particular object (or instance) of the class. Also the use of `self` in instance methods and `cls` in class methods is just convention in that the language will allow you to name it something different. – Andrew Allaire Oct 28 '21 at 17:17
  • You pass `self` so that you reference that instance of the class. There are times where you would want multiple instances of a class or even an instance of a class to call another instance of that same class. If you don't have a reason to reference anything else in that instance of the class, you can make a `staticmethod` that is part of the class but not tied to data in the class. – Rashid 'Lee' Ibrahim Oct 28 '21 at 17:17
  • [What is the rationale for closing "why" questions on language design?](https://meta.stackexchange.com/questions/170394/what-is-the-rationale-for-closing-why-questions-on-a-language-design) -- the question of _what Python syntax requires_ is asked and answered here over, and over, and over; and revisiting it is pointless. The question of whether it _should_ do that is simply off-topic. – Charles Duffy Oct 28 '21 at 17:18
  • Did any of the answers solve your problem? – MarioG8 Dec 22 '21 at 13:39

4 Answers4

0

Try it. It won't work because self is not defined. The name "self" is just established, but you could name it whatever you want. It refers to the object of the class on which the method is called.

Banana
  • 2,295
  • 1
  • 8
  • 25
0

Yes, you can do it but you have to mention it as a static method. Self represents the instance of the class. In simple words, self represents the object on which it is being executed. When you create an object, all variables change their values based on different object of same class. Every class needs object as it's argument because for different object, different values are assigned

0

self represents object of class on which method is called you don't always need to name it self[standard convention] any valid variable name is allowed in python to do this but it should be first argument of non-classmethods and should be replace self as in if you run this python will work as expected :

class Player:
    def __init__(hello, name):
        hello.name = name 
    
    def print_player_name(hello):
        print(hello.name)
Kunal Sharma
  • 416
  • 3
  • 12
0

It's very simple that's the official Python convention. In official docs we can read about it.

"Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention."

MarioG8
  • 5,122
  • 4
  • 13
  • 29