Are the critiques in "All the things I hate about Python" valid?

Antoon Pardon antoon.pardon at vub.be
Tue Feb 20 06:18:47 EST 2018


On 19-02-18 15:25, Steven D'Aprano wrote:
>
>> Ones like C++ has already tied itself itself up in knots just doing the
>> basics; I'm not sure how it would handle even my 1,3,5,7,9 type.
>>
>> But Python has classes and can do some of this stuff; how would it
>> handle a numeric type that is constrained to be whole numbers within
>> 0..9 inclusive?
> This becomes easy at run-time:
>
> class Digit(int):
>     def __new__(cls, arg):
>         instance = super().__new__(cls, arg)
>         if not 0 <= instance <= 9:
>             raise ValueError('argument is out of range')
>         return instance
>
> The above is obviously not a full-blown production-ready class. But it 
> illustrates the basic concept. This is the sort of thing that dynamic 
> languages excel at: enforcing constraints at run-time which are hard to 
> enforce at compile-time.

I don't see how dynamic languages are excelling here. Writing code that
ensures a number
of constraints can be done just as easily in a static language.

Personnally I would prefer the type system of Pascal and Modula2 with
their interval type
above a Digit class in python. For the simple reason that once you had
declared a variable
like this:
    x: 1 .. 10;

Each assignment to x would then implicitly do something like an assert
to checks the constraint,
so it would be impossible to ever assign 11 to x, without an error being
thrown.

There is no such possibility in Python. You can off course start with x
= Digit(5), but the language
won't stop you from doing x = 11 later.

I'm not proficient with C++, but IIUC, you could make a class in C++ and
have the constructor and
copy operator check for these kind of things. True it would be run-time
checks but that would
already be more than Python can give.

-- 
Antoon.



More information about the Python-list mailing list