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

Chris Angelico rosuav at gmail.com
Sat Sep 29 08:15:42 EDT 2018


On Sat, Sep 29, 2018 at 9:43 PM Steven D'Aprano <steve at pearwood.info> wrote:
>
> On Sat, Sep 29, 2018 at 10:32:16AM +1000, Chris Angelico wrote:
>
> > What should happen here?
> >
> > >>> import ctypes
> > >>> ctypes.cast(id(1), ctypes.POINTER(ctypes.c_int))[6] = 0
> > >>> 1
> >
> > Nothing here invokes C's undefined behaviour. Or what about here:
> >
> > >>> import sys; sys.setrecursionlimit(2147483647)
> > >>> def f(): f()
> > ...
> > >>> f()
>
> I'm not fussed about such edge cases. ctypes is not "Python", and other
> implementations don't support it. Mucking about with the interpreter
> internals is clear "Here be dragons" territory, and that's okay.

As are all the things that are "undefined behaviour" in C, like the
result of integer overflow in a signed variable. They are "Here be
dragons" territory, but somehow that's not okay for you. I don't
understand why you can hate on C for having behaviours where you're
told "don't do that, we can't promise anything", but it's perfectly
acceptable for Python to have the exact same thing.

AIUI, the only difference is that C compilers are more aggressive
about assuming you won't invoke undefined behaviour, whereas there are
no known Python interpreters that make such expectations.

ChrisA


More information about the Python-ideas mailing list