2

I am trying to learn python and am having difficulty in understanding why this class throws a error NameError: name 'self' is not defined .I have followed How to call a function within class? and added self, but this didn't help. Here is the code, can anyone please point out the reason.

class Back(object):    

    def square(self,x):
        y = x * x
        return y


    def main():
        self.square(3)


    if __name__ == "__main__":
    main()

And I am calling it using python square.py

2
  • All member functions must take the self object as their first argument. It also doesn't make much sense to have the main function be a member function. And you never even create an instance (an object) of the Back class. Lastly, for something simple like this, classes and objects doesn't really make much sense. Commented Oct 14, 2019 at 23:37
  • @Someprogrammerdude could you say more about "It also doesn't make much sense to have the main function be a member function" Commented Oct 14, 2019 at 23:44

1 Answer 1

3

You forgot to put the self in the method signature of main(). It should look like this

    def main(self):
        self.square(3)

Without that, self is in fact not defined in the scope of your method, so Python complains.

EDIT: also, as Some programmer dude mentions, your code never creates an instance of the class just executes main. There's also a problem with your indentation (probably a copy-paste error).

Try this instead:

class Back(object):    
    def square(self,x):
        y = x * x
        return y

def main():
    back = Back()
    print(back.square(3))


if __name__ == "__main__":
    main()

notice how main is defined at the root level (it's not indented like square). It's not part of the class this way and doesn't need self. You could make it a method of the Back class again like this:

class Back(object):    
    def square(self,x):
        y = x * x
        return y

    def main(self):
        print(self.square(3))


if __name__ == "__main__":
    back = Back()
    back.main()

Ok, this last one, it doesn't really make sens to do it this way, I admit. But I'm just trying to illustrate scope and the difference between function and methods in python (I think this logic may help the OP more, considering the question).

Sign up to request clarification or add additional context in comments.

4 Comments

@tekeshi2010 I tried adding self, in that case it complains NameError: name 'main' is not defined at the last line main()
edited, I hadn't noticed some other problems with the code on the first read (txs @Some programmer dude)
can't I have main() inside the class as posted originally?
You can if you want to (I edited with an example of that). Just know that by convention, the main function is a function (meaning it is defined at the root level, not as a method under a class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.