Python instance methods are the functions you write inside a class that give each object its behavior. When you define a method inside a class body and give it a first parameter named self, you are creating an instance method, the most common and most important kind of method in object-oriented Python. Every time you call a method on an object using dot notation, Python looks up that method on the object's class, passes the object as the first argument automatically, and executes the method body with full access to that object's attributes. This mechanism is so fundamental to how Python classes work that you have already been using instance methods since the moment you created your first class, even if the terminology was not yet explicit. The article on creating your first Python class introduced the basic syntax of class definitions and methods. This article zooms in on instance methods specifically, explaining what makes them distinct, how they receive and use the self parameter, and how to design them for clarity and reuse.
An instance method differs from a standalone function in one essential way: it is bound to an object. A standalone function receives only the arguments you explicitly pass to it. An instance method receives those same explicit arguments plus one additional, invisible argument: the object itself, always passed as the first parameter. Python handles this binding automatically, which is why you never write the object as an argument when you call a method. The method definition still declares the parameter, and the method body still uses it, but the caller never sees it. This design keeps method calls concise while giving every method full access to the object's internal state, a combination that makes object-oriented code both readable and powerful.
The relationship between instance methods and instance attributes is the core dynamic of object-oriented programming. An object stores its data in attributes that are typically set during initialization. Its instance methods read those attributes to make decisions, modify them to update the object's state over time, and sometimes return computed values based on them. Without instance methods, an object would be a passive container of data with no behavior. Without instance attributes, instance methods would have nothing to operate on and would be indistinguishable from ordinary functions. The two concepts depend on each other, and the self parameter is the thread that connects them.
How instance methods receive and use self
The self parameter is the first parameter of every instance method, and Python fills it in automatically whenever the method is called on an object. If you define a method with the signature def transfer(self, amount, target_account), you call it with only two arguments because Python supplies the object as self behind the scenes. The method body then uses self to access the object that received the method call, reading its attributes and potentially calling other methods on the same object through the same self reference.
Consider a simple banking class where the instance methods collaborate to manage an account's state:
class Account:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
self.history = []
def deposit(self, amount):
self.balance += amount
self.history.append(f"Deposited {amount}")The deposit method receives the Account object as self, adds the amount to the balance attribute, and records the transaction in the history list. The withdraw method checks a condition before modifying state:
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
self.history.append(f"Withdrew {amount}")
return True
return FalseBoth deposit and withdraw are instance methods. They each receive the Account object as self, read and modify its balance attribute, and append to its history list. The withdraw method additionally checks a condition before modifying state, returning a boolean that tells the caller whether the operation succeeded.
Calling instance methods and what happens under the hood
When Python encounters a method call like account.deposit(100), it performs a sequence of operations that transforms the dot-notation call into a function invocation with the object inserted as the first argument. Python first looks up the name deposit on the account object's class, not on the object itself. It finds the deposit function defined in the Account class. It then calls that function with account as the first argument and 100 as the second, which matches the method's parameter list where self receives the object and amount receives 100. The method body executes, and any return value propagates back to the caller.
This behind-the-scenes translation means you can technically call an instance method through the class name rather than through an instance, as long as you pass the object explicitly. Writing Account.deposit(account, 100) produces the same result as account.deposit(100). The class-level call form is rarely used in everyday code, but understanding it demystifies what self actually is. It is not magic. It is an ordinary function parameter that Python fills in for you when you use the more natural dot-notation calling style.
The automatic binding of self also means that instance methods cannot be called without an object. If you try to call Account.deposit(100) without passing an Account instance as the first argument, Python raises a TypeError because the method received one argument when it expected two. The error message tells you that you are missing the required positional argument self, which is Python's way of saying that an instance method needs an instance to operate on. This restriction is by design: instance methods exist to work with object state, and calling one without an object would be meaningless.
Designing instance methods that do one job well
The most important design principle for instance methods is that each method should do one clearly named job. A method called calculate_monthly_interest should calculate interest and return the result; it should not also update the balance, send an email notification, and write to a log file. Keeping methods focused on a single responsibility makes them easier to test in isolation, easier to reuse in different contexts, and easier to understand when you or someone else reads the code months later.
A well-designed instance method typically follows a predictable structure. It reads one or more attributes from self to gather the data it needs. It performs some computation or decision based on that data. It may modify attributes on self to update the object's state. It returns a result that the caller can use, or it returns nothing and relies on the state change as its effect. The method does not access global variables or modify objects other than self unless that is explicitly part of its documented responsibility.
Methods that grow beyond roughly twenty lines are often trying to do too many things at once. If you find a method that validates input, performs a calculation, formats output, and logs a result, consider splitting it into smaller methods that each handle one part of the workflow. A process_payment method might call validate_payment_details, calculate_fees, update_balance, and log_transaction, each as separate instance methods on the same object. The calling method reads like an outline of the process, and each helper method is short enough to understand at a glance.
Instance methods and the broader Python object model
Instance methods are the default method type, but Python supports two other kinds of methods that serve different purposes. Class methods, marked with a decorator, receive the class itself as their first parameter instead of an instance, and they are used for operations that need to know about the class but not about any particular object. Static methods, also marked with a decorator, receive no automatic first parameter at all and behave like regular functions that happen to live inside a class for organizational reasons. The articles on class methods and static methods later in this section explain when to use each alternative. For now, the rule of thumb is simple: if your method needs to read or modify attributes on a specific object, it should be an instance method with self as the first parameter.
The skills you built in the Python functions section transfer directly to writing instance methods. Parameter design, default values, return statements, and docstrings all work the same way inside a class as they do in standalone functions. The only addition is the self parameter and the dot-notation calling convention. If you are comfortable writing functions that accept parameters, perform logic, and return results, you already know everything you need to write instance methods.
A small example that captures a bound method reference demonstrates how self stays attached to the object even when the method is stored in a variable and called later:
checking = Account("Maya", 1000)
savings = Account("Maya", 5000)
transfer_method = checking.withdraw
transfer_method(200)
print(checking.balance)The withdraw method is stored in a variable while still bound to the checking account object. Calling it later with an amount of 200 deducts from the checking balance only, leaving the savings account untouched. The bound method remembers its object regardless of when or where it is called, which is a powerful pattern for callbacks and deferred execution.
Rune AI
Key Insights
- An instance method is a function defined inside a class that takes self as its first parameter and operates on a specific object.
- Python passes the object automatically when you call a method using dot notation on an instance.
- Instance methods can read and modify the object's attributes through self, and can call other methods on the same object.
- The self parameter is not a keyword but a universal convention that every Python developer follows.
- Instance methods are the default method type; use them unless you specifically need class-level or static behavior.
Frequently Asked Questions
What is an instance method in Python?
How is an instance method different from a regular function?
Can an instance method call another instance method on the same object?
Conclusion
Instance methods are the workhorses of object-oriented Python. They give objects their behavior by reading and writing instance attributes through the self parameter, and they let you design classes where data and the operations on that data travel together as a single unit. Every subsequent OOP topic, from class methods and static methods to inheritance and polymorphism, builds on the foundation that instance methods provide.
More in this topic
Python OOP Design Best Practices
Learn the best practices for designing Python classes, from the SOLID principles adapted for Python to practical guidelines for writing maintainable object-oriented code.
Object-Oriented Programming in Python Explained
Learn what object-oriented programming means in Python, why classes and objects matter, and how OOP helps you organize larger programs into manageable, reusable pieces.
Read and Write Large Files in Python
Learn how to read and write large files in Python without running out of memory, using chunked reading, line-by-line iteration, and buffered writes.