This article mainly introduces the _ slots _ usage in python. The example analyzes the functions of _ slots _ and related usage skills, for more information about how to use _ slots _ in python, see the example in this article. 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.
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.