Python Forum
Do lists of classes take up more space in memory?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do lists of classes take up more space in memory?
#2
Since each image always has the same attributes, you could save even more space using slots. Classes that use slots instead of __dict___ provide faster access and have a smaller memory footprint.
class CustomImage(): #my custom image class
    __slots__ = 'name', 'path', 'size', 'ext', 'avg'
    def __init__(self, name, path, size, rgb, ext="png"):
        self.name = name
        self.path = path
        self.size = size
        self.ext = ext
        self.avg = rgb
With slots you cannot add an attribute to the class. You can access or set attribute values, but I could not do custimg.rating = 4 because there is no slot for "rating".
If CustomImage is immutable you could also use a named tuple (import collections).
Reply


Messages In This Thread
RE: Do lists of classes take up more space in memory? - by deanhystad - Apr-10-2020, 01:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting an error while trying to process data, low memory when memory is not low? bkeith12 0 523 Dec-20-2024, 03:06 PM
Last Post: bkeith12
  Using my REPL to bisect numbers and lists with classes (PyBite #181) Drone4four 2 2,728 Sep-24-2020, 01:47 PM
Last Post: Drone4four
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,309 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  from global space to local space Skaperen 4 3,178 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 5,066 Mar-20-2019, 08:01 PM
Last Post: stillsen
  How to reallocating memory space in multithreading program in python ? siva 2 4,140 Mar-08-2018, 03:49 AM
Last Post: siva
  Using classes? Can I just use classes to structure code? muteboy 5 6,333 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020