1

I am a beginner in Python and have gone through some help material on __ symbol and my question is based on it.

I have executed the following in a Python 3.7 shell / interpreter:

 class __:

...     def strangeClassName():

...             print("Inside a function")

…

 inst = __()

which is not problematic at all. However, when I try to access the print statement using the function name(strangeClassName()), I am not able to get it. I mean - not able to see the print's output.

What is happening and how to access the function, as the call inst.strangeClassName did not help!

1 Answer 1

5

You must specify the first argument that represents the instance object for all instance methods. The name self is only convention and you can name it your way. But following the convention is the good practice.

Looks like this:

class __:
    def foo(self):
        print("inside")


inst = __()
inst.foo()

Output:

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

1 Comment

Thanks for your response. So, can I consider passing 'self' to every method as a best practice & is a must for instance methods (as against static methods) ?

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.