This content originally appeared on DEV Community and was authored by Jérôme Krell
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
🦄Hope this helps and have a great Day
Jerome
This content originally appeared on DEV Community and was authored by Jérôme Krell
Jérôme Krell | Sciencx (2022-03-28T11:11:41+00:00) Consume 50% less memory with your Python objects. Retrieved from https://www.scien.cx/2022/03/28/consume-50-less-memory-with-your-python-objects/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.