A monkey patch (also spelled monkey-patch, MonkeyPatch) is a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, etc.) without altering the original source code.

Plone community promotes conflict free way to do monkey patching using collective.monkeypatcher package.

Patching constants

Some modules (typically files) include constant definitions used throughout the package. Given that collective.monkeypatcher is intended to patch methods you’ll not be able to patch a constant straightforward. Instead you’ll have to make use of the handler option:

    description="Add new terabyte constant"

And your module should include this:

NEW_SIZE_CONST = {'kB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024, 'TB': 1024*1024*1024*1024}

patched_size_const = lambda : NEW_SIZE_CONST  # Now we have a callable method!

def apply_patched_const(scope, original, replacement):
    setattr(scope, original, replacement())

This way the original SIZE_CONST constant would be replaced by the result of the lambda function, which is our new constant.

Patching @property methods

If you are to patch a @property decorated method you can use the handler configuration option:

    description="Performance boost in foldercontents"

And your module should include this:

def items(self):
    ...  # The body of your patched method

def apply_patched_property(scope, original, replacement):
    # This is actually the same as apply_patched_const above
    setattr(scope, original, replacement())

patched_items = lambda : property(items)  # We get a @property decorated method!

This way the original items method would be replaced by the result of the lambda function, which is a @property decorated method written in a different way.