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

Steven D'Aprano steve at pearwood.info
Sat Sep 29 07:42:18 EDT 2018


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.

 
> Python has its own set of "well don't do that then" situations. In
> fact, I would say that *most* languages do.

Indeed, but that's not really what I'm talking about.

As I said earlier, Python's semantics are relatively simply in the sense 
that it is governed by a particular execution model (a virtual machine) 
that isn't hugely complex, but there are some corner cases which are 
complex. Overall Python is very rich and non-minimalist. I wouldn't 
describe it as "simple" except in comparison to languages like C++.

BASIC is a simple language, with only two scalar data types (strings and 
numbers) and one array type, and only a handful of fairly simple flow 
control tools: if, loops, GOTO/GOSUB. (Of course, modern BASICs may be 
more complex.)

Python has a rich set of builtin data types (ints, floats, strings, 
bytes, lists, tuples, dicts...), even more in the standard library, 
OOP, and on top of the familiar BASIC-like flow control (for 
loops, while loops, if...else) we have comprehensions, with statements 
and exception handling. (Did I miss anything?) But not GOTO :-)



-- 
Steve


More information about the Python-ideas mailing list