0
class Student:
    def __init__(self, name):
        self.name = name

I know why self is used in this code. This can take different students and make differnt attributes

student1 = Student()
student2 = Student()
...
studentn = Student()
------------------------------------

student1.name
student2.name
...
studentn.name

but I can't understand why this code below needs self parameter.

class Student:
    def study():
        print("I'm studying")


Student().study()

output

Traceback (most recent call last):
  File "C:/directory/test.py", line 12, in <module>
    Student().study()
TypeError: study() takes 0 positional arguments but 1 was given
cavalist
  • 85
  • 2
  • 8
  • 1
    Because when you call an instance method, the receiving object is passed as an argument to the method. If the method can't accept that object, you get an error. If you don't want the function to accept a `self` parameter, you don't have to make it an instance method. – khelwood Dec 14 '19 at 15:00
  • 1
    well, BTW your first code that you claim to understand, is not even valid. It will throw you the same error, just different numbers... `__init__ takes 2 positional arguments but 1 was given` – Tomerikoo Dec 14 '19 at 15:13

8 Answers8

3

The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called

More on the self variable here

emilaz
  • 1,722
  • 1
  • 15
  • 31
3

Because methods are passed the instance they're called on, regardless of if the method needs it or not.

If you don't want to need to have the parameter, make it a "static method":

class Student:
    @staticmethod
    def study():
        print("I'm studying")
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
2

you can use this code if not add 'self'

class Student:
    def study():
        print("I'm studying")


Student.study()

output: I'm studying

Muqadir
  • 21
  • 5
0

This is just how python works when you do instance.method(). Method receives the instance as the first parameter. Note that: Student().study() is equivalent to

student = Student()
student.study()
RafalS
  • 5,834
  • 1
  • 20
  • 25
0

Self is not a keyword, It's just a convention. Also, when you pass self to the method of a class you are referring to that particular object by using self(as a class can have multiple objects and all have access to that method), self gives you the option to use the attributes of the class in that method by using self.att . You can make the output of the function study as attribute like self.study="I am studying" that will better suit your purpose. You can read more here and here .

Vaibhav
  • 771
  • 6
  • 9
0

Guido Van Rossum (the maker of python) wrote a blogpost in which he told why explicit self has to stay.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Flair Sep 01 '21 at 01:56
0

The reason is because whenever a method is called on an object, Python automatically passes the method the object that called it.

Mustafa Kemal
  • 762
  • 1
  • 5
  • 11
-2

When creating function inside a class you need to pass self as argument:

class Student:
    def study(self):
        print("I'm studying")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Reznik
  • 2,663
  • 1
  • 11
  • 31