Python | Use of __slots__ Last Updated : 27 Dec, 2019 Comments Improve Suggest changes Like Article Like Report When we create objects for classes, it requires memory and the attribute are stored in the form of a dictionary. In case if we need to allocate thousands of objects, it will take a lot of memory space. slots provide a special mechanism to reduce the size of objects.It is a concept of memory optimisation on objects. Example of python object without slots : Python3 1== class GFG(object): def __init__(self, *args, **kwargs): self.a = 1 self.b = 2 if __name__ == "__main__": instance = GFG() print(instance.__dict__) Output : {'a': 1, 'b': 2} As every object in Python contains a dynamic dictionary that allows adding attributes. For every instance object, we will have an instance of a dictionary that consumes more space and wastes a lot of RAM. In Python, there is no default functionality to allocate a static amount of memory while creating the object to store all its attributes. Usage of __slots__ reduce the wastage of space and speed up the program by allocating space for a fixed amount of attributes. Example of python object with slots : Python3 1== class GFG(object): __slots__=['a', 'b'] def __init__(self, *args, **kwargs): self.a = 1 self.b = 2 if __name__ == "__main__": instance = GFG() print(instance.__slots__) Output : ['a', 'b'] Example of python if we use dict : Python3 1== class GFG(object): __slots__=['a', 'b'] def __init__(self, *args, **kwargs): self.a = 1 self.b = 2 if __name__ == "__main__": instance = GFG() print(instance.__dict__) Output : AttributeError: 'GFG' object has no attribute '__dict__' This error will be caused. Result of using __slots__: Fast access to attributes Saves memory space Comment More infoAdvertise with us Next Article Python | Use of __slots__ S Somil_singh Follow Improve Article Tags : Technical Scripter Python Python Programs Practice Tags : python Similar Reads Python Access Tuple Item In Python, a tuple is a collection of ordered, immutable elements. Accessing the items in a tuple is easy and in this article, we'll explore how to access tuple elements in python using different methods.Access Tuple Items by IndexJust like lists, tuples are indexed in Python. The indexing starts fr 2 min read Starred Expression in Python Starred expression (*) in Python is used to unpack elements from iterables. It allows for extracting multiple values from a sequence and packing them into a list or a tuple or even collecting extra arguments in a function. It can also be used in assignments to separate elements or merge iterables.Un 3 min read Shared Reference in Python Let, we assign a variable x to value 5, and another variable y to the variable x. Python3 x = 5 y = x When Python looks at the first statement, what it does is that, first, it creates an object to represent the value 5. Then, it creates the variable x if it doesn't exist and made it a reference to t 4 min read What is __Init__.Py File in Python? One of the features of Python is that it allows users to organize their code into modules and packages, which are collections of modules. The __init__.py file is a Python file that is executed when a package is imported. In this article, we will see what is __init__.py file in Python and how it is u 5 min read Memory Management in Lists and Tuples using Python In Python, lists and tuples are common data structures used to store sequences of elements. However, they differ significantly in terms of memory management, mutability, and performance characteristics. Table of ContentMemory Allocation in ListsMemory Allocation in TuplesComparison of Lists and Tupl 3 min read Like