Detailed tutorial on using the _ slots _ method in Python
This article describes how to use the _ slots _ method in Python. The _ slots _ method is an important built-in class method of Python. The Code is based on Python 2.x, for more information, see
Under normal circumstances, when we define a class and create a class instance, we can bind any attributes and methods to the instance, which is the flexibility of Dynamic Language. First define class:
?
1 2 3 |
>>> Class Student (object ): ... Pass ... |
Then, try to bind an attribute to the instance:
?
1 2 3 4 |
>>> S = Student () >>> S. name = 'Michael '# dynamically bind an attribute to the instance >>> Print s. name Michael |
You can also try to bind a method to the instance:
?
1 2 3 4 5 6 7 8 |
>>> Def set_age (self, age): # define a function as an instance method ... Self. age = age ... >>> From types import MethodType >>> S. set_age = MethodType (set_age, s, Student) # bind a method to the instance >>> S. set_age (25) # Call the instance method >>> S. age # Test Result 25 |
However, the method bound to an instance does not work for another instance:
?
1 2 3 4 5 |
>>> S2 = Student () # create a new instance >>> S2.set _ age (25) # try to call the Method Traceback (most recent call last ): File "<stdin>", line 1, in <module> AttributeError: 'student 'object has no attribute 'set _ age' |
To bind methods to all instances, You can bind methods to the class:
?
1 2 3 4 |
>>> Def set_score (self, score ): ... Self. score = score ... >>> Student. set_score = MethodType (set_score, None, Student) |
After binding a method to a class, all instances can call the following code:
?
1 2 3 4 5 6 |
>>> S. set_score (100) >>> S. score 100 >>> S2.set _ score (99) >>> S2.score 99 |
Generally, the above set_score method can be directly defined in the class, but dynamic binding allows us to dynamically add features to the class during the running process, which is difficult to implement in static languages.
Use _ slots __
However, what if we want to restrict the attributes of a class? For example, you can only add the name and age attributes to the Student instance.
To achieve the restriction, Python allows you to define a special _ slots _ variable when defining a class to restrict the attributes that can be added to this class:
?
1 2 3 |
>>> Class Student (object ): ... _ Slots _ = ('name', 'age') # Use tuple to define the names of attributes that can be bound. ... |
Then, let's try:
?
1 2 3 4 5 6 7 |
>>> S = Student () # create a new instance >>> S. name = 'Michael '# bind the property 'name' >>> S. age = 25 # bind the property 'age' >>> S. score = 99 # bind the attribute 'score' Traceback (most recent call last ): File "<stdin>", line 1, in <module> AttributeError: 'student 'object has no attribute 'score' |
Because 'score 'is not put in _ slots _, you cannot bind the score attribute. If you try to bind the score, you will get the AttributeError error.
Note that attributes defined by _ slots _ __slots _ only work for the current class and do not work for inherited subclass:
?
1 2 3 4 5 6 |
>>> Class GraduateStudent (Student ): ... Pass ... >>> G = GraduateStudent () >>> G. score = 9999 Try |
Unless _ slots __is also defined in the subclass, the attribute that can be defined by the subclass is its own _ slots _ plus the _ slots _ of the parent class __.