Questions tagged [slots]

`__slots__` is a python language feature to reduce size of object instances by dropping the builtin dictionary every python instance normally features in order to support dynamic assignment of attributes and replacing it with a fixed set of attributes (the 'slots').

__slots__ is a python language feature to reduce size of object instances by dropping the builtin dictionary every python instance normally features in order to support dynamic assignment of attributes and replacing it with a fixed set of attributes (the 'slots'). See: https://docs.python.org/2/reference/datamodel.html#slots

306 questions
1185
votes
14 answers

Usage of __slots__?

What is the purpose of __slots__ in Python — especially with respect to when I would want to use it, and when not?
Jeb
  • 15,939
  • 6
  • 34
  • 37
84
votes
5 answers

How does inheritance of __slots__ in subclasses actually work?

In the Python data model reference section on slots there is a list of notes on using __slots__. I am thoroughly confused by the 1st and 6th items, because they seem to be contradicting each other. First item: When inheriting from a class…
jathanism
  • 33,067
  • 9
  • 68
  • 86
41
votes
6 answers

How can dataclasses be made to work better with __slots__?

It was decided to remove direct support for __slots__ from dataclasses for Python 3.7. Despite this, __slots__ can still be used with dataclasses: from dataclasses import dataclass @dataclass class C(): __slots__ = "x" x: int However,…
Rick
  • 43,029
  • 15
  • 76
  • 119
37
votes
2 answers

How to access the slots of an S4 object in R

I'm working with wavelets on a program and I'm used the package wavelets to create the DWT of a time series using the function dwt. This function returns an object of class dwt, which is a S4 object with many slots: W, V, levels, filter, and so…
zaire90
  • 713
  • 2
  • 6
  • 11
30
votes
3 answers

Why am I getting an error about my class defining __slots__ when trying to pickle an object?

I'm trying to pickle an object of a (new-style) class I defined. But I'm getting the following error: >>> with open('temp/connection.pickle','w') as f: ... pickle.dump(c,f) ... Traceback (most recent call last): File "", line 2, in…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
24
votes
4 answers

Cannot inherit from multiple classes defining __slots__?

A certain situation in Python recently alarmed me, and its reason is still not completely clear after a little research. The following class definitions appear to work flawlessly and will produce what is intended: class A: __slots__ = 'a', 'b' class…
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
19
votes
2 answers

Get all __slots__ of derived class

I need to initialise all slots of an instance with None. How do I get all slots of a derived class? Example (which does not work): class A(object): __slots__ = "a" def __init__(self): # this does not work for inherited classes …
Knack
  • 1,044
  • 2
  • 12
  • 25
19
votes
1 answer

Python, __slots__, inheritance, and class variables ==> attribute is read-only bug

I have a big tree with hundreds of thousands of nodes, and I'm using __slots__ to reduce the memory consumption. I just found a very strange bug and fixed it, but I don't understand the behavior that I saw. Here's a simplified code sample: class…
Jonathan
  • 1,864
  • 17
  • 26
17
votes
3 answers

"Non-function value encountered for default slot." in Vue 3 Composition API component

MCVE https://github.com/hyperbotauthor/minvue3cliapp MCVE live https://codesandbox.io/s/white-browser-fl7ji I have a Vue 3 cli-service app, that uses composition API components with slots. The HelloWorld component renders the slots it receives in a…
hyperbotauthor
  • 307
  • 1
  • 4
  • 13
17
votes
2 answers

How does __slots__ avoid a dictionary lookup?

I've heard that __slots__ makes objects faster by avoiding a dictionary lookup. My confusion comes from Python being a dynamic language. In a static language, we avoid a dictionary lookup for a.test by doing a compile-time optimisation to save the…
Casebash
  • 114,675
  • 90
  • 247
  • 350
15
votes
1 answer

Are Python 3.11 objects as light as slots?

After Mark Shannon's optimisation of Python objects, is a plain object different from an object with slots? I understand that after this optimisation in a normal use case, objects have no dictionary. Have the new Python objects made the use of slots…
Jorge Luis
  • 813
  • 6
  • 21
15
votes
1 answer

Select all rows of a Vuetify data table with custom table body implementation

I can't figure how to implement the select all option for my data-table using Vuetify v2 when I have a custom implementation for the slot body. Here is a little example: