Python calling method without 'self' [duplicate]
Asked Answered
S

6

78

So I just started programming in python and I don't understand the whole reasoning behind 'self'. I understand that it is used almost like a global variable, so that data can be passed between different methods in the class. I don't understand why you need to use it when your calling another method in the same class. If I am already in that class, why do I have to tell it??

example, if I have: Why do I need self.thing()?

class bla:
    def hello(self):
        self.thing()

    def thing(self):
        print "hello"
Sclera answered 8/9, 2013 at 2:27 Comment(2)
Open a Python interpreter and run import this for an explanation of most of Python's language decisions.Chishima
@ColinValliant: Obscure and versatile doctrinal statements like "Now is better than never. Although never is often better than *right now.*" can only be interpreted by an enlightened Pythoness and don't really help answering the question.Isis
O
76

Also you can make methods in class static so no need for self. However, use this if you really need that.

Yours:

class bla:
    def hello(self):
        self.thing()

    def thing(self):
        print "hello"

static edition:

class bla:
    @staticmethod
    def hello():
        bla.thing()

    @staticmethod
    def thing():
        print "hello"
Onstad answered 8/9, 2013 at 4:38 Comment(6)
I know this is an older post, but would you mind to give an example of a scenario in which one would use a static method?Triplenerved
@Triplenerved Static methods operate on one or more static variables. For instance, you have a static variable keeping count of the number of instances created so far. A static method could then be used to, say, increment that counter.Ronni
@Triplenerved non-static methods can access static variables, but static methods can't access non-static variables.Ronni
@Developer, you don't need the self argument in thing definition. This is support for not using @staticmethod at all and just referring to the methods by the class name prefixHenpeck
Even if I do not use self , it works without using the @staticmethod decorator.Ebonize
@Ebonize that is correct. But it is a good practice to marc the method as static to show other developers it was intended to be static and consequently not having the 'self'Frontogenesis
B
15

One reason is to refer to the method of that particular class's instance within which the code is be executed.

This example might help:

def hello():
    print "global hello"

class bla:
    def hello(self):
        self.thing()
        hello()

    def thing(self):
        print "hello"

b = bla()
b.hello()
>>> hello
global hello

You can think of it, for now, to be namespace resolution.

Biostatics answered 8/9, 2013 at 2:32 Comment(0)
C
6

I have tried below code that declared method with out parameter in class and called method using class name.

class Employee:

    def EmpWithOutPar():
        return 'Hi you called Employee'

print(Employee.EmpWithOutPar())

output : Hi you called Employee

Copyboy answered 29/4, 2020 at 7:15 Comment(2)
what does the decorator @staticmethod from @Developers answers is then needed for?Rowlett
Just to clarify, the difference is that you call the class directly and do not instatiate it (emp = Employee()), thus you do not need the decorator (@staticmethod). However if one wants a class that you can instatiate as well as has the ability to call static methods you can use the decorator.Rowlett
V
2

The short answer is "because you can def thing(args) as a global function, or as a method of another class. Take this (horrible) example:

def thing(args):
    print "Please don't do this."

class foo:
    def thing(self,args):
        print "No, really. Don't ever do this."

class bar:
    def thing(self,args):
        print "This is completely unrelated."

This is bad. Don't do this. But if you did, you could call thing(args) and something would happen. This can potentially be a good thing if you plan accordingly:

class Person:
    def bio(self):
        print "I'm a person!"

class Student(Person):
    def bio(self):
        Person.bio(self)
        print "I'm studying %s" % self.major

The above code makes it so that if you create an object of Student class and call bio, it'll do all the stuff that would have happened if it was of Person class that had its own bio called and it'll do its own thing afterwards.

This gets into inheritance and some other stuff you might not have seen yet, but look forward to it.

Voiture answered 8/9, 2013 at 2:45 Comment(1)
I fail to understand how this is bad. thing(), foo().thing() and bar().thing() are obviously different functions; there’s no way you can take one for another.Roofdeck
F
2

To me, self like a scope definer, with self.foo() and self.bar indicating the function and the parameter defined in the class and not those defines in the other places.

Fukien answered 23/7, 2014 at 1:18 Comment(0)
M
0

There might be another function with the same name outside your class. self is an object reference to the object itself, therefore, they are same. Python methods are not called in the context of the object itself. self in Python may be used to deal with custom object models or something.

Mushro answered 8/9, 2013 at 2:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.