A method is a function that is attached to an object, while a function stands on its own. In practice: len(my_list) is a function call, and my_list.append(3) is a method call. The method receives the object it belongs to as its first argument automatically (usually named self), which is why you call it with a dot and do not pass the object yourself. That single difference explains most of the confusion, and the steps below show how to spot, write, and use each correctly.
What You'll Need
- Python 3 installed (any recent 3.x version works; labels and behavior are stable across them).
- A way to run Python: the interactive interpreter, IDLE, or any editor that runs
.pyfiles. - A basic grasp of variables and calling functions with parentheses.
Step-by-Step
- Start with a plain function. A function is defined with
defat module level and is called by its name. Example:def greet(name): return "Hi " + name, thengreet("Sam"). Nothing is attached to an object; you pass every argument explicitly. - Define a class and put a function inside it. A function defined inside a class body becomes a method of that class. Example:
class Greeter:then indenteddef greet(self, name): return "Hi " + name. The first parameter,self, refers to the instance. - Create an instance and call the method with a dot.
g = Greeter()theng.greet("Sam"). Notice you do not passgyourself; Python fills inselfautomatically. That automatic binding is the defining feature of a method. - Compare the two calls side by side.
greet("Sam")is a function call.g.greet("Sam")is a method call. Same logic, different binding. If you callGreeter.greet("Sam")on the class itself, it fails becauseselfis missing; you would needGreeter.greet(g, "Sam"). - Check whether a name is a function or a method. In the interpreter, run
type(len)and you get<class 'builtin_function_or_method'>, whiletype("abc".upper)shows a bound method. A quicker practical test: if you call it with a dot on an object (obj.name()), it is a method; if you call it by bare name (name()), it is a function. - Recognize built-in functions versus built-in methods.
len(),print(),sum(), andsorted()are functions."abc".upper(),[1,2].append(3), and{"a":1}.keys()are methods on string, list, and dict objects. The dot tells you which. - Write a function that takes an object when the operation is general. Use a function when the action applies to many types or when it is not naturally owned by one object. Example:
def double(x): return x * 2works on numbers, strings, and lists. - Write a method when the action belongs to the object's data. If the operation reads or changes the object's own attributes, make it a method inside the class. Example: a
BankAccountclass with adeposit(self, amount)method that updatesself.balance. - Remember static and class methods. Inside a class you can use
@staticmethodfor a function that takes no automatic first argument, and@classmethodfor one that receives the class (cls) instead of the instance. Both are still called with a dot, but they behave differently from ordinary instance methods. - Verify with a small script. Print the result of each call and confirm the output matches what you expect. If a method call raises
TypeErrorabout missing arguments, you likely forgotselfin the definition or called it on the class instead of an instance.
Common Mistakes
- Forgetting
selfin a method definition, then wondering why the call needs an extra argument. - Calling a method on the class (
ClassName.method()) instead of on an instance, which skips the automatic binding. - Assuming every dotted name is a method: module functions like
math.sqrt()are functions accessed through a module, not methods on an object. - Using a function where a method is expected, such as passing
obj.methodwithout parentheses and getting the bound method object instead of its result. - Confusing
list.sort()(a method that changes the list in place and returnsNone) withsorted()(a function that returns a new list).
Tips
- Read the call site: a dot before the name usually means a method; a bare name means a function.
- Name methods with verbs that describe the object's behavior (
save,close,append) and keep general-purpose helpers as functions. - Use
help(obj.method)orhelp(function)in the interpreter to see the signature and whetherselfis implied. - When in doubt, write the function first. Convert it to a method only when it clearly belongs to one class's data.
Troubleshooting
If you get TypeError: method() missing 1 required positional argument: 'self', you called the method on the class rather than an instance, or you defined it without self. Create an instance and call through it. If you get AttributeError: 'X' object has no attribute 'y', the method does not exist on that object; check the class definition and spelling. If a call returns <bound method ...>, you forgot the parentheses. If results differ between list.sort() and sorted(), remember the first mutates in place and returns None. Restart the interpreter after editing a class definition, since old instances keep the old methods.
Frequently Asked Questions
Is every method a function?
In Python, methods are functions defined inside a class and accessed through an instance or class. So yes, a method is a kind of function, but not every function is a method.
Why does a method need self?
self is the instance the method operates on. Python passes it automatically when you call obj.method(), so you do not supply it yourself.
Can I call a method without an instance?
Yes, by calling it on the class and passing the instance explicitly, such as ClassName.method(instance, args). Static and class methods are the usual exceptions.
Is len() a function or a method?
It is a built-in function. You call it as len(obj), not obj.len().
What is a bound method?
It is a method already tied to a specific instance, which is what you get when you write obj.method without parentheses. Calling it runs the method on that instance.
Do static methods count as methods?
They are defined in a class and called with a dot, but they do not receive self or cls automatically, so they behave much like plain functions placed inside the class namespace.