8.python Face Object Part.8 (__slots__ property)

Source: Internet
Author: User

What is the role of the __slots__ attribute? Why do you have the __slots__ property?

First of all, what is __slots__, __slots__ is a class variable, the value inside it can be a string, can be a list, can be a tuple. (If the value of this property is a string, the object generated by this class can have only one property, __slots__).

So why use the __slots__ property?

This issue also starts with the __dict__ property of each object.

When using the. Point to access the property the essence is to access the __dict__ property Dictionary of the class or object (the dictionary of the class is shared, and each instance is independent), if a class generates a lot of objects, each time an object is generated, a __dict__ property dictionary is generated for that object. If there are many classes, many dictionaries will be generated, which will consume a lot of memory space.

If you have a class with a few attributes, but there are many instances, in order to save memory you can use __slots__ to replace the instance of __dict__ ~


When you define __SLOTS__, __slots__ uses a more compact internal representation for the instance. Instead of defining a dictionary for each instance, the instance is constructed from a small, fixed-size array.

This is similar to a tuple or list. The attribute names listed in __slots__ are internally mapped to the specified small label for this array.


!! One drawback of using __slots__ is that we can no longer add new properties to the instance, only those that are defined in __slots__. This is a very noteworthy!!


Two. About the idea of using the __slots__ property.

    1. You cannot add a new property to an object after you use the __slots__ Property! You can only use the property name defined in the __slots__ property in the Class!!!

    2. Classes that have the __slots__ attribute defined will no longer have some features of the normal class, such as multiple inheritance. In most cases, you should make it clear that some classes that create millions of objects in a program need to use the __slots__ property and define __slots__ only on classes that are often used as data structures.


Three. A number of additions to __slots__.

A common misconception about __slots__ is that it can be used as an encapsulation tool to prevent users from adding new properties to an instance. Although the use of __slots__ can achieve this goal, but this is not its original intention. More is used as a memory optimizer tool.



Four. Demonstration of how to use __slots__.

Example 1:

Class Foo:

__slots__= ' x '

F1=foo ()

F1.x=1

f1.y=2# Error

Print (f1.__slots__) #f1不再有__dict__

Class Bar:

__slots__=[' x ', ' Y ']

N=bar ()

n.x,n.y=1,2

n.z=3# Error


Example 2: (Validation of __slots__ characteristics)

Class Foo:

__slots__=[' name ', ' age ']

F1=foo ()

F1.name= ' Alex '

F1.age=18

Print (f1.__slots__)

F2=foo ()

F2.name= ' Egon '

F2.age=19

Print (f2.__slots__)

Print (foo.__dict__)

#f1与f2都没有属性字典__dict__了, unified __slots__ tube, save memory



This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1918562

8.python Face Object Part.8 (__slots__ property)

Related Article

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.