Python OOP – Method vs Function and the Mystery of ‘self’

I just realized how much I didn't know about Python Object-Oriented Programming. I thought I knew the basics, but a few days ago, while going through a Python course, I found out I was wrong. Before I forget what I’ve learned, I wanted to write this blog post and share it with you.
In this blog post, we’ll cover the difference between functions and methods, and what exactly ‘self’ means in Python. So, let’s get to it.
Functions vs Method
class MyClass:
def say_hello():
print('Hello')In this snippet, we’ve defined a Class called MyClass with a function named say_hello. But here’s a question for you - what do you call say_hello? Is it a function or a method?
It’s a common misconception to think that simply defining a function inside a Class automatically makes it a method. However, the distinction lies in how the function is accessed.

- Function - When you define a function inside a class, it’s just a regular function until it’s accessed through an instance of the class.
- Method - When you access that function via an instance of the class (e.g., `obj.say_hello'), it becomes a method. This is Continue reading



