Example of _ slots _ usage in python
This example describes how to use _ slots _ in python. Share it with you for your reference. The specific analysis is as follows:
After _ slots _ is defined, the attribute names that can be assigned on the instance will be limited to the specified names. Otherwise, AttributeError is triggered. This restriction can prevent others from adding new attributes to the existing instance.
Instances of classes using _ slots _ do not use dictionaries to store data. Instead, a more compact array-based data structure is used.
Using _ slots _ in a program that creates a large number of objects can significantly reduce memory usage and usage time.
?
1 2 3 4 5 |
Class Account (object ): _ Slots _ = ('name', 'balance ') Class Test (object ): Def _ init _ (self, name ): Self. name = name |
I hope this article will help you with Python programming.