Skip to content

Instantly share code, notes, and snippets.

@Cosmicoppai
Created November 9, 2024 00:00
Show Gist options
  • Save Cosmicoppai/e7d4dc6375662edaa9c9dfae4b624fdb to your computer and use it in GitHub Desktop.
Save Cosmicoppai/e7d4dc6375662edaa9c9dfae4b624fdb to your computer and use it in GitHub Desktop.
python slot
class Car:
"""
Regular classes use a __dict__ for attributes
"""
def __init__(self):
self.brand_name = "hyundai"
class ECar:
__slots__ = ("brand_name", ) # this will prevent declaration of new slots
__dict__ = {"brand_name": "kia"}
def __init__(self):
self.brand_name = "kia"
if __name__ == "__main__":
import tracemalloc
tracemalloc.start()
a = Car()
# a.price = 1000
print("current memory: {} \n peak memory: {}".format(*tracemalloc.get_traced_memory()))
tracemalloc.stop()
del a
tracemalloc.start(10)
b = ECar()
# b.price = 1000
# print(b.__dict__)
print("current memory: {} \n peak memory: {}".format(*tracemalloc.get_traced_memory()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment