Adding data types with constraints
In this section, we will look at an example of adding constraints to data types and checking constraints along with type checking. There might be scenarios where we would like to create an integer variable and restrict its length to two digits or to create a string and restrict its length to 10 characters and more. With this example, let’s explore how to add such constraints or restricts during the static type checking.
In this example, let’s create a typecheck
class with only two methods to check an integer and a string. While checking these data types, let’s also add a few more constraints within the method definition:
class typecheck:
- In the following method, let’s check that the input variable is not an integer or its length is greater than two, and return
False
when the condition is true, and returnTrue
when the condition is false:def intcheck(self,inputvalue): &...