Property if we want to limit the instances of how to do? For example, only add Student instance name
and age
attributes.
Then we add _slots_ variables inside the Student class
E.g:
class Student (Object): __slots__ is = ( ' name ' , ' Age ' ) # a tuple attribute name definition allows binding
Then, we try:
S = Student >>> () # create a new instance >>> s.name = ' Michael ' # add attribute 'name' >>> s.age = 25 # add attribute 'Age' >>> = s.score 99 # Add property 'Score' Traceback (MOST Recent Last Call): File " <stdin> " , Line. 1, in <Module1> AttributeError: ' Student ' Object attribute has NO ' Score '
When we add the property to s score instance, an error occurs, then the limit _slots_ variable success. Because the 'score' property is not placed in _slots_.
Note: The __slots__
attributes defined only for the current class instance acts on the subclass inherits is ineffective unless define subclasses _slots_.