[Python-ideas] Pre-conditions and post-conditions

Steven D'Aprano steve at pearwood.info
Wed Aug 29 07:51:57 EDT 2018


On Wed, Aug 29, 2018 at 09:23:02AM +0200, Jacco van Dorp wrote:
> Op wo 29 aug. 2018 om 03:59 schreef Steven D'Aprano <steve at pearwood.info>:
> 
> > On Tue, Aug 28, 2018 at 07:46:02AM +0200, Marko Ristin-Kaufmann wrote:
> > > Hi,
> > > To clarify the benefits of the contracts, let me give you an example from
> > > our code base:

[snip long sequence of @decorator(lambda) calls]

> > Thanks for your example Marko. I think that is quite close to the
> > ugliest piece of Python code I've ever seen, and I don't mean that as a
> > criticism of you for writing it or the icontract library's design.
> >
> >
> What, really ? 

I said *close* :-)


> Well, it clearly shows you teach python and don't look much
> at code written by people who taught themselves.

I didn't mean to compare it to code written by beginners. I meant 
professional quality. And I didn't mean it was *bad* code.

Python is a remarkable elegant and pretty language, but there are some 
things that aren't a good fit to the existing syntax. Contracts are one. 
We can't easily write code in a declarative style like Prolog:

    sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y)

(X is a sibling of Y when there exists some Z who is a parent of X and 
the same Z is the parent of Y); we have to re-write it in a procedural 
style. Some programming styles aren't a natural fit to a given syntax.


> I taught myself, and the
> first .py file I created was over a 1000 lines, and contained the GUI in a
> 4-deep nested global dictionary, since I'd never seen a style guide at that
> point. (I got better)

So far, nothing you describe is *necessarily* ugly or bad code.

The std lib contains at least one file well over 1000 lines, and while 
it is complex code, its not ugly code by any means. And I couldn't judge 
the elegance of the dict unless I saw it and the alternatives :-)


-- 
Steve


More information about the Python-ideas mailing list