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

Using __slots__ with SQLAlchemy model

I am using SQLAlchemy on my new project and would like to use __slots__ with models (in beta version without alchemy, __slots__ were necessary because a large number of objects was created). But I am unable to combine them with SQLAlchemy…
jOOsko
  • 608
  • 6
  • 9
4
votes
1 answer

Qt signal and slots: are reference arguments copied?

In qt framework, most library signals and slots use pointers as parameters. I was wondering, If I create a signal-slot "structure" that takes a reference as the parameter instead of the pointer, will the whole parameter be copied, or just 4 bytes…
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
4
votes
2 answers

@property not working after adding __slots__

How can I get slots to work with @property for the class below. I have several thousand instances of below class which is causing memory issues and so I added the slots I created instances with data and then add location information later to the…
zsh
  • 103
  • 1
  • 5
3
votes
1 answer

Why slots=True makes a dataclass to ignore its default values?

This code works as expected: from dataclasses import dataclass @dataclass(slots=False, init=False) class Test: field: str = "default value" print(Test()) # outputs "Test(field='default value')" However, if I change slots to True, it throws…
enkryptor
  • 1,574
  • 1
  • 17
  • 27
3
votes
2 answers

Is it valid Python to use a __slots__ dictionary for initialization purposes?

While searching for a convenient method to initialize slots, I had the stupid? idea of wrongly? using __slots__ dictionaries as shown below. Note: There is a related question on SO where I've previously posted my idea as answer, but I though it…
P. B.
  • 587
  • 6
  • 12
3
votes
0 answers

Is it a good practice to use __annotations__.keys() for __slots__?

Recently I learned about __slots__, and started using them on my codes. And I came up with this idea of using annotations for slots. class Foo: bar: int baz: str __slots__ = __annotations__.keys() This allows me to change attributes…
WieeRd
  • 792
  • 1
  • 7
  • 17
3
votes
1 answer

How to create read-only slots?

Slots are writable by default: >>> class A: __slots__ = ('x',) ... >>> list(vars(A)) ['__module__', '__slots__', 'x', '__doc__'] >>> vars(A)['x'] >>> a = A() >>> a.x = 'foo' >>> del a.x How to create read-only slots,…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
3
votes
1 answer

descriptor for objects doesn't apply to object, using __slots__

Good morning, I'm having a problem trying to use __ slots __ in one of my classes. I want to use it to optimize the creation of multiple instances of the Classifier object. I'm using Python 3.7.1. This Works class Classifier() : def…
3
votes
2 answers

Pass variables through to parent component

Child component: Vue.component('v-data', { template: `
`, computed: { visibleData(){ return [1,2,3]; }, }, }); In parent component I…
Alex
  • 66,732
  • 177
  • 439
  • 641
3
votes
1 answer

Why is __weakref__ removed by default when __slots__ is used?

I think the main purpose of __slots__ is to save the memory usage by allowing to specify properties explicitly, instead of using __dict__ allowing dynamic property assignment on the instances. So I somehow understand why __dict__ is removed by…
czheo
  • 1,771
  • 2
  • 12
  • 22
3
votes
0 answers

Using Slots or slot-scopes in v-for loops to access properties?

I'm having a difficult time understanding slots for some reason and why they should even be used. The only reason I can think of that would be nice for usuage is if we can reference specific properties within a v-for loop of an element and output…
Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
3
votes
2 answers

Qt-Signal not connecting in virtual machine

I develop and compile on RedHat 7.4 with GCC 4.8.5 and Qt 4.8.5. The code has to be statically linked. Then executed on a virtual machine running Scientific Linux release 6.7. memcpy-Wrap is used in order to prevent dependencies on newer GLIBC >=…
Tobias
  • 141
  • 12
3
votes
2 answers

Why does using __slots__ slow down this code?

I read in Usage of __slots__? that using __slots__ in Python can actually save time. But, when I tried to find the time taken using datetime, the results were contrary. import datetime as t class A(): def __init__(self,x,y): self.x = x …
Ayush Shridhar
  • 115
  • 4
  • 11
3
votes
1 answer

response of `vars(self)` is missing attributes

I have defined a class and need most of its attributes handed over to a function for processing. So I thought instead of creating a huge mess and naming them all I'd do processSomething(vars(self)) and hand over a nice dictionary with all attributes…
Chris
  • 5,788
  • 4
  • 29
  • 40
3
votes
1 answer

Is it a good idea to use slots in python?

Is using slots in python a good pythonic technique or it is a habit coming from the C family of languages? I heard that they enhance performance of the program and reduce memory consumption, espessially when many objects are created. In principle,…
freude
  • 3,632
  • 3
  • 32
  • 51