Adding a method to an existing object instance in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, you can add behavior to one specific object instance at runtime instead of changing the whole class. The important detail is that a plain function assigned to an instance is just an attribute until it is bound correctly as a method. If you want the function to receive self automatically, bind it with types.MethodType or the descriptor protocol.
Instance-Level Method Injection
Suppose you have a class and want one object to gain a custom method.
If you assign howl directly, Python treats it as a plain function object stored on the instance.
Calling fido.howl() now fails because the function is not automatically bound to fido.
Bind the Function as a Method
The cleanest way is types.MethodType.
Output:
Now self is supplied automatically because the function has been bound to that specific instance.
Alternative: Use __get__
The descriptor protocol can do the same job more explicitly.
This works because functions in Python are descriptors. Calling __get__ binds the function to the instance and returns a bound method.
Why You Might Do This
Adding a method to one instance can be useful for:
- tests and quick mocks
- runtime customization of one object
- attaching behavior in plugin-style systems
- experiments in interactive sessions
But it is usually not the best design for ordinary application code. If every instance should gain the behavior, modify the class instead.
Add a Method to the Class When It Belongs Everywhere
If all instances need the method, patch the class, not one object.
That affects every current and future Dog instance, which is often what you actually want.
Watch Out for __slots__
Not every instance can accept new attributes. If the class defines __slots__ without a __dict__, arbitrary instance attributes cannot be added.
An object of Cat cannot accept a new meow_loudly method unless the class design allows dynamic attributes.
Treat It as a Tool, Not a Default Pattern
Per-instance method injection is essentially a form of monkey-patching. It is powerful, but it can surprise readers because the object's interface is no longer obvious from the class definition alone.
That tradeoff is acceptable in tests, adapters, and debugging sessions. It is much riskier in long-lived production code where maintainability matters more than runtime cleverness.
Common Pitfalls
- Assigning a plain function to an instance and expecting automatic
selfbinding. - Using per-instance method injection when the behavior really belongs on the class.
- Forgetting that
__slots__may prevent adding new attributes. - Making production code harder to reason about with too much runtime patching.
- Confusing instance attributes with bound methods.
Summary
- You can add a method to one Python instance at runtime.
- Use
types.MethodTypeor__get__to bind the function correctly. - Assigning the function directly is not enough if you want automatic
selfhandling. - Patch the class instead if the method should exist on every instance.
- Be aware that
__slots__can prevent per-instance method injection.

