Tarun Dhiraj
March 1, 2020

Slots in python dict

Posted on March 1, 2020  •  One minute  • 158 words

The special attribute __slots__ allows you to explicitly state which instance attributes you expect your object instances to have. The benefits of using __slots__ are as follows:

The space savings is from the following:

Example:

  class Person(object):
    __slots__ = ('a', 'b')

Now, you can’t have dynamic properties in person class ie. You can only access and modify properties a and b.

  person = Person()
  person.a = 'a'
  person.b = 'b'
  person.c = 'c'.  -> gives error

Requirements:

Let's Connect

Different platforms highlight my different sides

Berlin
Germany