Usually Python devs store data structures in dictionaries. A more efficient way is to use "slotted objects". A slotted object is a class which has the slots attributed defined. In the interest of space, the typical class dict attribute is removed.
Example of a slotted object:
class DemoObject(object): # Note we subclass "object" to remove the __dict__ attr
__slots__ = ('a', 'b', 'c')
demo1 = DemoObject()
demo1.a = 1
demo1.b = 2
Slotted objects have a couple of advantages over dicts:
Faster attribute access (approx 15% speedup)
Savings on storage space especially when multiple objects are created (over 50% savings)
Usually Python devs store data structures in dictionaries. A more efficient way is to use "slotted objects". A slotted object is a class which has the slots attributed defined. In the interest of space, the typical class dict attribute is removed.
Example of a slotted object:
Slotted objects have a couple of advantages over dicts:
Caveats: