Access attributes defined in __slots__

Is it possible to access instance attributes defined in __slots__? Like you would access them by subscribing self.__dict__["attr"] in a dynamic class where you don’t define __slots__?

Background:
I overload the __getattr__ and __setattr__ methods, where I access attributes using __dict__["attr"]. Now I want to make my classes more memory efficient.

Thanks!

super().__getattr__/super().__setattr__ is going to fall back to the normal attribute resolving system. You can also use this instead of self.__dict__

If for some reason this doesn’t work for you (which is quite unlikely), you can manually call the __get__ and __set__ methods on the descriptor objects inside of the type(self).__dict__["attr"], but doing this correctly is a bit tricky.

1 Like