Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Unbound Method: What It Is and How It Changed

An unbound method in Python 2 is the object you get when you access an instance method through the class. Python 3 replaces it with a plain function. See what changed and how to handle the difference.

~4 min readPython
#python#method binding#class methods#python 2 vs 3
Illustration of a Python unbound method being accessed from a class, showing the transition from Python 2 to Python 3.

When you access an instance method through the class itself in Python 2, you get an unbound method. For example:

python
class Greeter: def greet(self, name): return f"Hello, {name}" # Access via the class method = Greeter.greet print(type(method)) # In Python 2: <type 'instancemethod'>

An unbound method behaves like a function that expects the instance as its first argument. You can call it by passing an instance explicitly:

python
g = Greeter() Greeter.greet(g, "Alice") # Works in both Python 2 and 3

In Python 2, Greeter.greet is an unbound method object, distinct from a plain function. This distinction was a source of confusion for many developers.

How Python 3 Changed Unbound Methods

Python 3 removed the unbound method type. Accessing a method via the class now returns a plain function:

python
class Greeter: def greet(self, name): return f"Hello, {name}" method = Greeter.greet print(type(method)) # <class 'function'>

For valid calls, the behavior is the same: you pass the instance as the first argument. The most visible difference is the object type. In Python 2, an unbound method also enforced that the first argument was an instance of the class; in Python 3, no such check exists. This simplification makes the language more consistent and removes a special case that had little practical benefit.

Bound vs Unbound Methods: Practical Differences

A bound method is created when you access a method through an instance. It automatically captures the instance, so you don't pass self explicitly:

python
g = Greeter() bound = g.greet print(bound("Bob")) # "Hello, Bob"

An unbound method (or a plain function in Python 3) requires you to pass the instance manually. The practical difference matters when you inspect method types or when you pass methods around as callbacks.

For example, inspect.ismethod returns True for a bound method in both Python 2 and 3, but it returns True for an unbound method only in Python 2. In Python 3, inspect.ismethod(Greeter.greet) is False, while inspect.isfunction is True.

Common Scenarios Where This Distinction Matters

You see this distinction whenever you intentionally access a method through the class rather than an instance. It often comes up in metaprogramming and decorators, or when you need to pass SomeClass.method to code that will supply the instance at call time.

Another common scenario is passing a method as a callback. If you pass Greeter.greet to a function that expects a callable, you must provide the instance at call time. This is different from passing g.greet, which already has the instance bound.

Compatibility Considerations for Python 2 and 3

If you maintain legacy code that still supports both Python 2 and Python 3, you need to be aware of the type difference. Code that relies on inspect.ismethod to detect unbound methods will behave differently. For example:

python
import inspect class A: def method(self): pass if inspect.ismethod(A.method): print("It's a method") else: print("It's a function")

In Python 2, this prints "It's a method"; in Python 3, it prints "It's a function". To write compatible code, rely on callable behavior rather than the inspect.ismethod result, or add an inspect.isfunction check for the Python 3 case. In legacy projects, libraries like six provide helpers to abstract over these differences.

Performance and Runtime Considerations

Creating a bound method object has a small runtime cost because Python allocates a new object each time you access the method through an instance. If you call a method repeatedly in a hot loop, you can avoid this overhead by storing the unbound method (or function) and passing the instance explicitly:

python
method = Greeter.greet for _ in range(1000): method(g, "Alice")

This avoids allocating a bound method object on each iteration. The performance difference is usually small, but it may matter in tight loops. However, this style is less readable and should be used only when profiling indicates a bottleneck.

Choosing Between Instance Methods, Class Methods, and Static Methods

The concept of unbound methods is closely related to how you define and call methods. Python offers three method types:

Method typeFirst argumentAccess via classTypical use
Instance methodselfReturns a function (or unbound method in Python 2)Operations that require instance state
Class methodclsReturns a bound method to the classOperations that need class-level state
Static methodnoneReturns a plain functionOperations that don't depend on class or instance

Choose an instance method when the logic depends on instance attributes. Use a class method when the logic needs class-level data or when you need a factory. Use a static method when the logic is independent of both. Understanding how these methods appear when accessed via the class helps you predict their behavior in callbacks and higher-order functions.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.