Example of _ slots _ usage in python

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.