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

Richard Damon Richard at Damon-family.org
Tue Feb 20 13:05:34 EST 2018



> On Feb 20, 2018, at 8:58 AM, Chris Angelico <rosuav at gmail.com> wrote:
> 
>> On Wed, Feb 21, 2018 at 12:53 AM, Antoon Pardon <antoon.pardon at vub.be> wrote:
>> In C++ I can do something like:
>> 
>>  SomeClass MyVar;
>> 
>> And after that the kind of possible assignments to MyVar are constraint. It
>> makes the runtime throw an error when somewhere the program tries to assign
>> something to MyVar that isn't allowed by SomeClass.
>> 
>> You can't put such constraints on names in Python.
>> 
>> In C++ I can do some like:
>>    Some_Class: MyVar;
>> 
>> And after that, It will be impossible to assign a value to MyVar that
>> doesn't meet the
>> constraints imposed by the constructor/copy operator. You have put
>> somekind of
>> contract on the name MyVar, that limits the kind of things assignable to
>> it. You
>> can't put such constraints on a name in Python.
> 
> Okay. Now create a constraint on a name in C++ such that it can only
> accept integers representing A.D. years which, on the Gregorian
> calendar, are leap years. (Using a dedicated integer-like type is
> permitted.) It must accept all multiples of four, except those which
> are multiples of one hundred, unless they're also multiples of four
> hundred.
> 
> That's what Steve asked for. Can you do it? Or is the C++ type system
> not flexible enough for that?
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Such a class would be fairly trivial to write in C++, or I suspect Python.

In C++ you would have a constructor and possibilities an assignment operator that takes an integer, tests it for validity and throw/asserts if it is incorrect. (Depending on other requirements, you might allow or not implicit conversions)
It probably also has a constructor and an assignment operator that takes a ‘LeapYear’ and just uses it.
To be usable, it will need something to allow you to get the integer value out, might be implicit or explicit.

The big issue with such a type, and why it doesn’t make much sense as a type, is that there is very little that can be done with such a type, as there is no interesting operation for which the type is at least mostly closed under, at best it is closed under +400*n



More information about the Python-list mailing list