On 20 February 2018 at 11:18, Antoon Pardon <antoon.par...@vub.be> wrote: > 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.
All that is saying is that in Pascal, variables have types, whereas in Python values have types but variables (names ;-)) don't. It's true, but not particularly important in toy examples like this. In larger scale programs, tracking the "type" of what gets assigned to a variable can be really useful, and carefully managed types can help with this. Pascal/Modula2 (and C/C++) have the compiler do this, Python has a separate tool (MyPy). Early C compilers used to have linters that did some checks outside of the compiler - maybe someday Python will build MyPy into the compiler (although I suspect not). Also, tracking "types" is only half the battle. There are probably very few people who have never mistakenly assigned NULL to a pointer in C that shouldn't be null. Or even in Java, which is supposed to not allow things like that. And tracking const types in C is widely acknowledged to be a mixed blessing at best. Where does "type" end and "documented constraint or programming contract" start? > 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. That (run-time checks) is exactly the same as Python gives. In C++, variables have types so you could ensure that you could only assign a value of your digit type to a given variable, but as I said above that's a separate point. It's somewhat unrelated (scoping is a different topic than assignment) but you can do { int x = 2; { char *x = "hello"; } } in C, so names can have different types even in C (it's just variables, or names within a specific scope, that have types associated with them). Summary: Different programming languages have different semantics. Which really isn't that surprising... Paul -- https://mail.python.org/mailman/listinfo/python-list