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

Steven D'Aprano steve at pearwood.info
Wed Aug 29 03:28:59 EDT 2018


On Wed, Aug 29, 2018 at 05:52:46PM +1200, Greg Ewing wrote:
> Wes Turner wrote:
> >    I'm going to re-write that in a pseudo-Eiffel like syntax:
> 
> Maybe some magic could be done to make this work:
> 
>      def __init__(self, img: np.ndarray, x: int, y: int, width: int,
>              height: int) -> None:
> 
>          def __require__():

The problem with this idea is that methods and functions are not 
declarations, but executable code. This __require__ function doesn't 
exist except while the __init__ method is running. So it can't be called 
before the __init__, it can't be called *automatically* (you need to 
call it yourself, from inside the __init__), and it can't be inherited.

Of course with sufficient compiler magic of course the compiler could 
special case these methods and do whatever we want, but that seems like 
it would be just as much work but much uglier than using dedicated 
syntax.


-- 
Steve


More information about the Python-ideas mailing list