DEV Community

Jérôme Krell
Jérôme Krell

Posted on

Consume 50% less memory with your Python objects

By default, in Python, objects have a dict attribute.
It's a dictionary used to access the object variables per key.

It is useful to allow dynamic variables' creation.

🚩However this flexibility can lead to creation of new object variables, when misspelled. Python will create a new variable with the given name.

With slots, we can specifically declare data variables.
Then Python will allocate space for them in memory and skip the creation of the dict attribute.

⛔It also forbids the creation of any object's variable which are not declared in the slots attribute.

By using slots, you also decrease the memory used by your class instances.

Slots can also be used in dataclasses if you're using python3.10 or higher. Simply add slots=True to the decorator.

⚡It makes a huge difference if you're creating lots of objects!

from dataclasses import dataclass

# https://pypi.org/project/Pympler/
from pympler.asizeof import asizeof

# Dataclass with slots
@dataclass(frozen=True, slots=True)
class SmallObjectWithDataclass:
    first_name: str
    last_name: str

# Class with slots
class SmallObject:
    __slots__ = ["first_name", "last_name"]

    def __init__(self, first_name, last_name) -> None:
        self.first_name: str = first_name
        self.last_name: str = last_name

# Class with no slots
class BiggerObject:
    def __init__(self, first_name, last_name) -> None:
        self.first_name: str = first_name
        self.last_name: str = last_name


p = SmallObjectWithDataclass("Jerome", "K")
print(asizeof(p))  # Output: 160 Bytes
p2 = SmallObject("Jerome", "K")
print(asizeof(p2))  # Output: 160 Bytes
p3 = BiggerObject("Jerome", "K")
print(asizeof(p3))  # Output: 392 Bytes
Enter fullscreen mode Exit fullscreen mode

🦄Hope this helps and have a great Day
Jerome

Top comments (2)

Collapse
 
naruaika profile image
Naufan Rusyda Faikar

I'm curious, how about the access performance? Could you do the measurement for me as I'm not a Python developer, but rather someone interested in Python?

Collapse
 
jeromek13 profile image
Jérôme Krell • Edited

Hey
There is also a performance gain when accessing / initializing the Object, see example. Hope that answers your question

import timeit
print(timeit.timeit("p3 = SmallObject('Jerome', 'K')", "from __main__ import SmallObject")) 
# Output: 0.13634319999982836
print(timeit.timeit("p3 = BiggerObject('Jerome', 'K')", "from __main__ import BiggerObject")) 
# Output: 0.166356599998835
Enter fullscreen mode Exit fullscreen mode