I know that python used duck typing, but I was wondering whether it is possible to enforce type validation for class variables, __slots__
in particular.
for example -
class Student:
def __init__(self, name):
self.name = name
class Class:
__slots__ = ('class_representative',
'var_2',
'var_3',
'...', # Assume many more variables below
)
def __init__(self, *args, **kwargs):
self.class_representative = kwargs.get('cr')
self.var_2 = kwargs.get('v2')
self.var_3 = kwargs.get('v3')
... # Assume many more variables below
In the above example, how do I make sure that whenever any object gets assigned to the class_representative
variable, it should always be of type Student
?
Is something like the following possible?
class Class:
__slots__ = ('class_representative': Student,
'var_2',
'var_3',
'...', # Assume many more variables below
)