Python calling method without 'self'

PythonClassSelf

Python Problem Overview


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"

Python Solutions


Solution 1 - Python

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"

Solution 2 - Python

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.

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

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

Solution 6 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSynaps3View Question on Stackoverflow
Solution 1 - PythonDeveloperView Answer on Stackoverflow
Solution 2 - PythonBleeding FingersView Answer on Stackoverflow
Solution 3 - PythonAli AlkhatibView Answer on Stackoverflow
Solution 4 - Pythonfrankwang4strangersView Answer on Stackoverflow
Solution 5 - PythonkarthiView Answer on Stackoverflow
Solution 6 - PythonD4NI3LSView Answer on Stackoverflow