A method in programming is a function that belongs to an object or a class, which means it can act directly on that objects own data. In object-oriented code, an object bundles together some data and the actions you can take on it; those actions are its methods. A list object, for example, has a method to add an item to itself. The defining trait is ownership: a method is attached to an object and operates on it, whereas a plain function stands on its own.
How methods work
You call a method using dot notation: you name the object, a dot, then the method. The method runs in the context of that specific object and can read or change its data.
// a method belongs to an object and acts on it
text = "hello world"
shout = text.upper() // upper() is a method on the string object
print(shout) // prints HELLO WORLD
numbers = [3, 1, 2]
numbers.sort() // sort() is a method that rearranges this list
upper() knows which text to uppercase because it was called on text. sort() rearranges that particular list. Each method operates on the object it was called on, which is the whole point.
Method versus function
This is the distinction people most want clarified, so here it is plainly.
| Aspect |
Function |
Method |
| Belongs to |
Nothing, it stands alone |
An object or class |
| Called as |
function(args) |
object.method(args) |
| Operates on |
Only what you pass it |
The objects own data, plus arguments |
| Needs an object? |
No |
Yes, it runs in the context of one |
A function is a standalone block of reusable logic. A method is that same idea but bound to an object, with built-in access to that objects data. Every method is a kind of function; not every function is a method. If functions themselves are new, start with how to write a function.
The self or this keyword
Inside a method, the language gives you a way to refer to the object the method was called on. In Python it is self; in JavaScript and many other languages it is this. That keyword is how a method reads and updates the right objects data rather than some other objects.
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count = self.count + 1 // self is this specific counter
Each Counter has its own count, and increment always changes the count of whichever counter it was called on.
How to use methods well
- Name methods as verbs or actions, like
save, send, or calculateTotal, since they do something.
- Keep each method focused on one job; if it does too much, split it into smaller methods.
- Use methods to act on the objects own data rather than reaching deep into other objects.
- Reach for plain functions when behavior does not belong to any particular object.
- Do not force object orientation; some problems and languages are cleaner with standalone functions.
What to skip
- Skip treating every function as a method. Not all code is object-oriented, and standalone functions are often clearer.
- Skip giant methods. A method spanning hundreds of lines is hard to test and reuse; break it up.
- Skip reaching across objects to grab data when the method should just ask the other object to act.
- Skip confusing naming. A method named
getUser should fetch, not also send an email as a side effect.
FAQ
What is the difference between a method and a function?
A function stands alone and works only on what you pass it. A method belongs to an object or class and can act on that objects own data. Every method is a function, but not every function is a method.
Why are methods called with a dot?
Dot notation links the action to the object it runs on. Writing object.method() tells the language which objects data the method should operate on, which is what distinguishes a method from a standalone function.
What does self or this mean inside a method?
It refers to the specific object the method was called on. Python uses self and JavaScript uses this. The keyword lets the method read and change the correct objects data rather than any other objects.
Do all languages have methods?
Languages with objects and classes do, which is most popular languages today. Some purely procedural or functional styles favor standalone functions instead, so methods are common but not universal.
Where to go next
Learn how to write a function, understand what an object in programming is, and see what a parameter is.