Skip to content

Patching Classes

Angus Hollands edited this page Apr 21, 2021 · 2 revisions

Literary provides a patch function which can monkey-patch existing classes with new methods, which is useful to break up a large class into annotate-able sections, e.g.

Here is a class. It has a do_that() method, and a do_the_other_thing() method!

class SomeClass:
    def do_this(self):
        pass

    def do_that(self):
        pass

    def do_the_other_thing(self):
        pass

becomes

Here is a class:

class SomeClass:
    def do_this(self):
        pass

It has a do_that() method

@patch(SomeClass)
def do_that(self):
    pass

and a do_the_other_thing() method!

@patch(SomeClass)
def do_the_other_thing(self):
    pass

At build-time (and import time), these patch decorators disappear (through AST rewriting), and there is no import to literary; literary is a build-time only dependency.

Notebook Development

Development of Literate notebooks using the IPython extension.

Package Development

Building packages from a collection of Literate notebooks.

Literary Development

About Literary

Clone this wiki locally