On this page
types.MethodType
Prev: types.MethodDescriptorType | Up: types | Next: types.new_class
Rel: types.AsyncGeneratorType, types.BuiltinFunctionType, types.BuiltinMethodType, types.CapsuleType
Ref: docs.python.org — types.MethodType
Tags: #public #python3 #stdlib #types
Import
import types
from types import MethodType
Signature & Overview
types.MethodType(function, instance, /)
Create a bound instance method object.
Usage
import types
class Box:
def __init__(self, v): self.v = v
b = Box(10)
bound = types.MethodType(lambda self: self.v * 2, b)
assert bound() == 20
Gotchas
- Dynamically binds a function to a single object instance.
REPL Inspect
from types import MethodType
help(MethodType)
Linked from
Related notes
- tuts/python3/stdlib_module/types/MethodDescriptorType Linked here; links to this page; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/new_class Linked here; links to this page; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/AsyncGeneratorType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/BuiltinFunctionType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/BuiltinMethodType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CapsuleType Linked here; shared tags: #stdlib, #types
- tuts/c/stdlib/stdint_stdbool_and_limits shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CellType shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/ClassMethodDescriptorType shared tags: #stdlib, #types