slots

Using Python descriptors with slots

耗尽温柔 提交于 2021-02-20 06:10:05
问题 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 descriptor class in order to be able to store values in the class instance which invokes the descriptor object. The usual solution would look like the one below but will not work since "dict" is no longer defined when "slots" is invoked in the C class: class MyDescriptor

How does inheritance of __slots__ in subclasses actually work?

喜夏-厌秋 提交于 2020-01-27 03:22:18
问题 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 without __slots__ , the __dict__ attribute of that class will always be accessible, so a __slots__ definition in the subclass is meaningless. Sixth item: The action of a __slots__ declaration is limited to the class where it is defined. As a result,

Qt signal and slots: are reference arguments copied?

余生长醉 提交于 2020-01-23 02:15:30
问题 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 (32-bit system) like in a regular c++ reference? I am asking this because I noticed something when I create a signal/ slot methods with the reference parameter. When I then connect them, the autocomplete mechanism in QTCreator doesn't hint me with

Qt Signals and Slots - nothing happens

笑着哭i 提交于 2019-12-25 07:40:01
问题 I'm currently trying to connect a QML Signal to a C++ Slot unsuccessfully. I just had a look on several other examples but I think i didn't got it with how to get the root object of a qml document... My problem is, it seems like the signal will be sent from the qml file, but not received in the cpp file. There are no errors when I execute this code. //Counter.h #ifndef COUNTER_H #define COUNTER_H #include <QObject> class Counter : public QObject { Q_OBJECT private: int counter; public:

Qt5: Though connected, Signal emmited but Slot not being called

北战南征 提交于 2019-12-25 01:35:32
问题 So I have a Qt class MyQtClass , and a QOject inherited class Sender . I want to access the Ui from the Sender class (Which by the way only consists static members), so I set up the static Sender& instance() , static void emitSignal() functions and the QSignal Q_SIGNAL void mySignal() in the Sender class (See the code below). In the Qt-class-header MyQtClass.h I've set up the QSlot Q_SLOT void mySlot() . I connect those two slots in the main.cpp ( const bool connected = QObject::connect(

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

可紊 提交于 2019-12-22 05:01:00
问题 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__ = [] 回答1: Simply speak, you just cannot do it. As stated in Documentation, Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations

Dynamically change __slots__ in Python 3

↘锁芯ラ 提交于 2019-12-17 20:23:03
问题 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 = A() b.x = 1 # OK b.y = 1 # AttributeError (why?) b was created after __slots__ of A had changed, so Python, in principle, could allocate memory for b.y . Why it didn't? How to properly modify __slots__ of a class, so that new instances have the modified

__setattr__ versus __slots__ for constraining attribute creation in Python

霸气de小男生 提交于 2019-12-14 02:29:24
问题 I've been reading about how make Python classes less dynamic, specifically by not allowing users to dynamically create new attributes. I've read that overloadding __setattr__ is a good way to do this , and __slots__ is not the way to go. One post on this last thread actually suggests that __slots__ can break pickling. (Can anyone confirm this?) However, I was just reading the whatsnew for Python 2.2, and the attribute access section actually suggests using __slots__ for the very purpose of

lisp, CLOS: adding a slot to the process class

不问归期 提交于 2019-12-12 20:19:10
问题 My program is getting errors with multithreading, so I want to expand the with-lock-grabbed macro to keep track of the stack of locks a process acquires. I want to do this by simply adding a slot to process to store the lock-stack. Unfortunately, I don't understand how to add a slot at runtime without destroying what's already there. ensure-class completely redefines the class. I don't want this, since I don't know what other slots process already has. How can I add a slot? In particular, I

How do I access an unknown instance's slot using a string?

邮差的信 提交于 2019-12-12 19:32:03
问题 Problem Given an instance, inst and a string attr containing the name of a slot, how can I obtain the value of the slot attr on inst ? Of course, if attr were a symbol rather than a string, I would typically just use (slot-value inst attr) , but it seems I need the package information to properly call intern (see below). Minimal example (defpackage :pack1 (:use :common-lisp) (:export :*inst*)) (in-package :pack1) (defclass temp-class () ((temp-slot :initarg :temp-slot))) (defvar *inst* (make