In a previous post, I detailed how to maintain encapsulation using Python’s property. In this piece, I go through how/why to manage and apply validation to class attributes in an object-oriented fashion by means of a fairly plausible example.
A type is the parent class of class, therefore any class is actually a sub-type of type. The following are equivalent:
a = int(8)
a = 8
type(a) # python knows to create an int without being explicit
int
The point of implementing custom attribute types is (in my case), for validation. The general pattern for creating a class that serves as a type to validate instance attributes is as follows (for a descriptor):