[Python-ideas] Why is design-by-contracts not widely adopted?

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Sep 25 06:08:30 EDT 2018

Angus Hollands writes:

 > yes I'd pass in some kind of 'old' object as a proxy to the old object
 > state.

Mostly you shouldn't need to do this, you can copy the state:

    def method(self, args):
        import copy
        old = copy.deepcopy(self)

This is easy but verbose to do with a decorator, and I imagine a bunch
of issues about the 'old' object with multiple decorators, so I omit it
here.  You might want a variety of such decorators.  Ie, using
copy.copy vs copy.deepcopy vs a special-case copy for a particular
class because there are large objects that are actually constant that
you don't want to copy (an "is" test would be enough, so the copy
would actually implement part of the contract).  Or the copy function
could be an argument to the decorator or a method on the object.

More information about the Python-ideas mailing list