On 20/02/2018 12:11, Paul Moore wrote:
On 20 February 2018 at 11:18, Antoon Pardon <antoon.par...@vub.be> wrote:
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.
I don't know if that counts. In Pascal (and presumably Ada) then all the
gubbins need to make this work properly:
var x: 1..10;
x = 10;
x = x + 1; { error? }
would already be in place. Trying to emulate that within a language is
complex and unwieldy, and someone has to know how to do it, and then do
it. If someone else creates an add-on for it, then those tend to be
large and cumbersome (because they have to have every conceivable bell
and whistle). And it's an extra dependency.
The end result: if I wanted to sit down right now and have a variable
and/or type in Python or C++ that is constrained to be within 1..10,
then I couldn't. I'd have to use a generic integer type.
And in fact, in Python, I couldn't even do that, as I can assign
anything at all to x.
This is not necessarily undesirable: part of the point of dynamic
languages is being informal and having less discipline imposed so that
things can get done more rapidly. But enforced discipline can also be
useful.
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).
That's scope; Python has that too. (Although not C's block scopes and
its bizarre rules which mean that you can have up 5 different 'x'
meanings even within the same block. And an unlimited number of 'x'
scopes within the same function.
(This is allowed in C: int L; L: L=10; goto L; )
--
bartc
--
https://mail.python.org/mailman/listinfo/python-list