Quick actions

cmd+k|ctrl+k

Navigation

Languages

Monkey-patching a new method to an object

Snippet info

Language

Python

Visibility

public

Author

klassmann

Created

2019-01-31T01:21:41Z

Updated

2019-01-31T01:21:41Z

import types

class MyClass(object):
    def __init__(self, value):
        self.value = value


c = MyClass(42)


def print_value(self):
    print(self.value)

c.print_value = types.MethodType(print_value, c)

c.print_value()
INFO