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
12
votes
1 answer

Is there a way to get the slots of a class?

I have a class like this one (defclass shape () ((color :initform :black) (thickness :initform 1) (filledp :initform nil) (window :initform nil))) Is there a function in common-lisp how to get a list of those slots if i only know instance of…
Micky
  • 125
  • 1
  • 7
12
votes
6 answers

Measure Object Size Accurately in Python - Sys.GetSizeOf not functioning

I am trying to accurately/definitively find the size differences between two different classes in Python. They are both new style classes, save for one not having slots defined. I have tried numerous tests to determine their size difference, but…
Zoran Pavlovic
  • 1,166
  • 2
  • 23
  • 38
10
votes
4 answers

How to dynamically change __slots__ attribute?

Suppose I have a class with __slots__ class A: __slots__ = ['x'] a = A() a.x = 1 # works fine a.y = 1 # AttributeError (as expected) Now I am going to change __slots__ of A. A.__slots__.append('y') print(A.__slots__) # ['x', 'y'] b =…
DLunin
  • 1,050
  • 10
  • 20
9
votes
1 answer

TypeError when calling super() in dataclass(slots=True) subclass

I am trying to call a a superclass method from a dataclass with slots=True in Python 3.10.5. from dataclasses import dataclass @dataclass(slots=True) class Base: def hi(self): print("Hi") @dataclass(slots=True) class Sub(Base): …
LeopardShark
  • 3,820
  • 2
  • 19
  • 33
9
votes
3 answers

Python 3.6.5 "Multiple bases have instance lay-out conflict" when multi-inheritance of classes having __slots__

If I run this code, I'v got the subject error message. But why? And how to avoid it getting the C class having its parents slots? class A(): __slots__ = ['slot1'] class B(): __slots__ = ['slot2'] class C(A, B): __slots__ =…
xsubira
  • 474
  • 1
  • 5
  • 14
9
votes
3 answers

Using Python descriptors with slots

I want to be able use python descriptors in a class which has the slots optimization: class C(object): __slots__ = ['a'] a = MyDescriptor('a') def __init__(self, val): self.a = val The problem I have is how to implement the…
Radu M.
  • 1,271
  • 2
  • 14
  • 19
9
votes
1 answer

How to pickle and unpickle objects with self-references and from a class with slots?

What is a correct way to pickle an object from a class with slots, when this object references itself through one of its attributes? Here is a simple example, with my current implementation, which I'm not sure is 100 % correct: import…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
8
votes
1 answer

TypeError when using super() in a dataclass with slots=True

I have a dataclass with (kind of) a getter method. This code works as expected: from dataclasses import dataclass @dataclass() class A: def get_data(self): # get some values from object's fields # do some calculations …
enkryptor
  • 1,574
  • 1
  • 17
  • 27
8
votes
2 answers

Equality of Python classes using slots

Another question provides a nice, simple solution for implementing a test for equality of objects. I'll repeat the answer for context: class CommonEqualityMixin(object): def __eq__(self, other): return (isinstance(other,…
new name
  • 15,861
  • 19
  • 68
  • 114
8
votes
2 answers

Using __slots__ under PyPy

I have this simple code that helped me to measure how classes with __slots__ perform (taken from here): import timeit def test_slots(): class Obj(object): __slots__ = ('i', 'l') def __init__(self, i): self.i = i …
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
8
votes
1 answer

Can __setattr__() can be defined in a class with __slots__?

Say I have a class which defines __slots__: class Foo(object): __slots__ = ['x'] def __init__(self, x=1): self.x = x # will the following work? def __setattr__(self, key, value): if key == 'x': …
bavaza
  • 10,319
  • 10
  • 64
  • 103
7
votes
1 answer

Slots does not work on a html web component without shadow dom

I have a html web component without shadow dom and I try to add a slot. For some reason it does not work. I expected it to switch "Foo bar" to "Hello world" but that does not happen. Does slots only works with shadow dom and a template? How can I…
Jens Törnell
  • 23,180
  • 45
  • 124
  • 206
7
votes
1 answer

__slots__ conflicts with a class variable in a generic class

I've got conflicts between Python's typing system and __slots__. Here is a small reproducible example. from typing import TypeVar, Generic, Sequence T = TypeVar("T") class TestGeneric(Sequence, Generic[T]): __slots__ = ("test",) def…
Eli Korvigo
  • 10,265
  • 6
  • 47
  • 73
7
votes
2 answers

"TypeError: native Qt signal is not callable" with custom slots

The Environment I am running an Anaconda environment with Python 3.4. I am using PyCharm as my IDE. The Objective I am trying to make a pyQt4 QPushButton connect to a custom function: button.clicked().connect([method reference or…
Xorgon
  • 498
  • 1
  • 5
  • 20
7
votes
1 answer

S4 classes: arguments passed to new() don't go into their slots

I'm building an R package with S4 classes, and I'm having trouble with the new function. I have a class called Configs setClass("Configs", slots = list( burnin = "numeric", chains = "numeric", features = "numeric", iterations =…
landau
  • 5,636
  • 1
  • 22
  • 50
1
2
3
20 21