• Latest
  • Trending
Speed Up Your Python classes with slot

Speed Up Your Python classes with slot

May 9, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Thursday, 16 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Speed Up Your Python classes with slot

by ITECHNEWS
May 9, 2022
in Data Science, Leading Stories
0 0
0
Speed Up Your Python classes with slot

The special attribute __slots__ allows you to explicitly state which instance attributes you expect your object instances to have, with the expected results:

  1. faster attribute access.
  2. space savings in memory.

The space savings is from

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

  1. Storing value references in slots instead of __dict__.
  2. Denying __dict__ and __weakref__ creation if parent classes deny them and you declare __slots__.

When we create an object from a class, the attributes of the object will be stored in a dictionary called __dict__. We use this dictionary to get and set attributes. It allows us to dynamically create new attributes after the creation of the object.

Let’s create a simple class Person that initially has 2 attributes first_name and last_name. If we print out __dict__ of the object, we will get the key and value of each attribute. Meanwhile, we also print __dict__ of the class which will be needed later. After that, a new attribute reviewer is added to the object, and we can see it in the updated __dict__.

class Person:
     def __init__ (self, first_name: str, last_name: str):
         self.first_name = first_name
         self.last_name = last_name

if __name__ == " __main__":
     person = Person("Chidozie", "Okafor")
     print(person. __dict__ )
  {'first_name': 'Chidozie', 'last_name': 'Okafor'}
    print(Person. __dict__ )
{' __module__': ' __main__', ' __doc__': None, ' __init__': <function __init__ at 0x10e9cd140>}

The issues with dictionary is memory consumption and also access dictionary involves hashing, Dictionary is in fact a hash map. The worst case of the time complexity of get/set in a hash map is O(n).

slots provide a special mechanism to reduce the size of objects.It is a concept of memory optimisation on objects.

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 attribute

To create a slot, all we need to do is to add __slots__ field or slots=True if we are using dataclass.

class PersonSlot:
     __slots__ = ["first_name", "last_name"]
     def __init__ (self, first_name: str, last_name: str):
         self.first_name = first_name
         self.last_name = last_name

if __name__ == " __main__":
     person = Person("Chidozie", "Okafor")
     print(person. __dict__ )
  {'first_name': 'Chidozie', 'last_name': 'Okafor'}
    print(Person. __dict__ )
{' __module__': ' __main__', ' __slots__': ['first_name', 'last_name'], ' __init__': <function __init__ at 0x10432b140>, ' __doc__': None}

To avoid repetition of variable names, we can use dataclasses to create a class that will automatically create slots for us.

from dataclasses import dataclass

@dataclass(slots=True)
class PersonSlot:
      first_name: str
      last_name: str

if __name__ == " __main__":
   person = PersonSlot("Chidozie", "Okafor")
   print(person. __dict__ )
   print(PersonSlot. __dict__ )

Advantages of using slots:

  1. Slots are faster than __dict__.
  2. Slots are more memory efficient.
  3. Slots are more secure.
  4. Slots are more convenient.
  5. Slots are more readable.
  6. Slots are more efficient.

Disadvantages of using slots:

  1. Slots are not compatible with Multiple inheritance and Mixins.
  2. Slots are not compatible with metaclasses.
  3. Slots are not compatible with ABCs.
  4. Slots are not compatible with pickling.
  5. Slots are not compatible with copy.
  6. Slots are not compatible with deepcopy.

Let’s see how fast slots are compare to normal dictionary.

import timeit
from dataclasses import dataclass
from functools import partial

@dataclass(slots=False)
class Person:
    first_name : str
    last_name: str

@dataclass(slots=True)
class PersonSlot:
    first_name : str
    last_name : str

def get_set_delete(person: Person | PersonSlot):
     person.first_name = "Raphael"
     _ = person.first_name
     del person.first_name

def get_percentage_of_performance():
    person = Person("Chidozie", "Okafor")
    person_slot = PersonSlot("Chidozie", "Okafor")
    no_slots = min(timeit.repeat(partial(get_set_delete, person), number=100, repeat=3))
    slots = min(timeit.repeat(partial(get_set_delete, person_slot), number=100, repeat=3))
    print(f"No Slots: {no_slots}") No Slots: 3.3914000596269034e-05

print(f"Slots: {slots}") Slots: 1.8151000404031947e-05
    print(f"Percentage of performance: {(slots / no_slots) * 100}") Percentage of performance: 53.520670180175635

if __name__ == " __main__":
    get_percentage_of_performance()

There are so many things we can do with slots to help improve the speed of our code. try as many as possible and share with others.

Happy coding!!!!

Source: Chidozie C. Okafor
Tags: Speed Up Your Python classes with slot
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version