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
Python lets you attach behavior to an object at runtime, including adding a method to one existing instance. The cleanest way is to bind a function with types.MethodType, which turns a normal function into a bound method that receives self automatically.
Bind a Function to One Instance
Instance-level method injection works because most Python objects store attributes in a writable instance dictionary. MethodType handles the method binding so the function behaves like a normal instance method.
The method exists only on ava. The mina instance is unchanged.
That makes this technique useful when you want per-object customization without altering the whole class.
Understand the Difference Between Instance and Class Patching
If you assign the function to the class, every instance gets the new method:
That is normal monkey patching at the class level. It affects the entire type, which can be powerful but also risky in shared code.
By contrast, MethodType is precise. It changes one object and leaves the rest of the program alone.
Practical Cases for Runtime Method Injection
This pattern is most useful when the change is local and temporary:
- adding test behavior to a single object
- adapting one instance from a third-party library
- instrumenting one object for debugging
Here is a simple test double example:
This can be convenient in focused tests where introducing a full subclass or mock framework would add more noise than value.
Know the Limits
Not every object allows new instance attributes. If a class uses __slots__ without an instance dictionary, arbitrary attribute assignment can fail.
Trying to add a new method to LockedUser("Ava") will raise AttributeError, because there is no room for arbitrary per-instance attributes.
Also remember that runtime-patched methods are harder for readers and tools to discover. A teammate inspecting the class definition will not see behavior added later in some distant module.
Common Pitfalls
The most common mistake is assigning a plain function directly to an instance and expecting automatic binding:
That stores a function object, but calling it may not behave like a proper bound method. MethodType is the safer approach.
Another issue is overusing dynamic patching in production code. It can make control flow difficult to follow and turn debugging into a search for hidden side effects. If the behavior is permanent, subclassing, composition, or dependency injection is usually a better design.
Finally, be careful when patching objects from frameworks or extension types. Some built-in and C-backed objects do not support arbitrary instance attributes at all.
Summary
- Use
types.MethodTypeto add a bound method to one existing Python object. - Instance-level patching affects only that object, while class-level patching affects every instance.
- This technique is useful for tests, adapters, and temporary instrumentation.
- It may fail on objects that do not allow arbitrary attributes, such as some
__slots__classes. - For long-term behavior changes, prefer clearer designs such as subclassing or composition.

