metaclass

PyQt5: Issues with a Mixin with a metaclass within a derived QWidget class

扶醉桌前 提交于 2019-12-08 03:36:53
问题 I'm having issues trying to add a mixin with a metaclass to a class whose base is a QWidget. I'm aware that PyQt5 supports cooperative multiple inheritance and if my MixIn class has no metaclass then things work fine. However, if it has a metaclass - whether it be the pyqtWrapperType metaclass shared by QWidgets or a derived metaclass, then I receive the following error: Process finished with exit code -1073741819 (0xC0000005) The code for the rest of the script runs, but the QWidget does not

In Python, decorate two methods with the same name to distinguish them

心已入冬 提交于 2019-12-08 01:36:35
问题 I am writing a framework to be used by people who know some python. I have settled on some syntax, and it makes sense to me and them to use something like this, where Base is the Base class that implements the framework. class A(Base): @decorator1 @decorator2 @decorator3 def f(self): pass @decorator4 def f(self): pass @decorator5 def g(self) pass All my framework is implemented via Base's metaclass. This set up is appropriate for my use case because all these user-defined classes have a rich

how does metaclass work with the MRO list when super() is called?

家住魔仙堡 提交于 2019-12-07 18:34:06
问题 I'm really confused by the following code sample: class Meta_1(type): def __call__(cls, *a, **kw): # line 1 print("entering Meta_1.__call__()") print(cls) # line 4 print(cls.mro()) # line 5 print(super(Meta_1, cls).__self__) # line 6 rv = super(Meta_1, cls).__call__(*a, **kw) # line 7 print("exiting Meta_1.__call__()") return rv class Car(object, metaclass=Meta_1): def __new__(cls, *a, **kw): print("Car.__new__()") rv = super(Car, cls).__new__(cls, *a, **kw) return rv def __init__(self, *a, *

The built-in keyword type means a function or a class in python?

半腔热情 提交于 2019-12-07 14:32:46
问题 In most posts, people often say type is a built-in function if it is provided with one argument, and it is a metaclass if provided with 3 arguments. But in python's doc, the signature of type is: class type(object) class type(name, bases, dict) So, does it means type is a built-in class and not a built-in function even if it is provided with one argument? 回答1: type is called a "metaclass" because it's a class that produces other classes (AKA types). It behaves like a normal class. In

how do i subclass threading.Event?

北慕城南 提交于 2019-12-07 12:25:46
问题 In Python 2.7.5: from threading import Event class State(Event): def __init__(self, name): super(Event, self).__init__() self.name = name def __repr__(self): return self.name + ' / ' + self.is_set() I get: TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str Why? Everything I know about threading.Event I learned from: http://docs.python.org/2/library/threading.html?highlight=threading#event-objects What does it mean when it says that threading.Event()

Setting metaclass of wrapped class with Boost.Python

回眸只為那壹抹淺笑 提交于 2019-12-07 04:44:18
问题 I have an Event class defined in C++ that I expose to Python using Boost. My scripts are expected to derive from this class, and I'd like to do some initialization whenever a new child class is defined. How can I set the metaclass of the exposed Event class such that whenever a Python script derives from this class, the metaclass could do the required initialization? I would like to avoid having to explicitly use a metaclass in the scripts... class KeyboardEvent(Event): # This is what I want

How to convert a string with the name of a class to the class type itself?

╄→гoц情女王★ 提交于 2019-12-06 23:17:19
问题 In order to store a class name in a log file I converted the description of a class type to a string: let objectType: NSObject.Type = Object.self let str = String(describing: objectType) However, I do not succeed in back conversion of str to a variable of type NSObject.Type to use it in a generic method. How could I do this? 回答1: You can get your class back from string , but you need to use your project's module name while getting class name. If you don't use your module name then it will

深刻理解Python中的元类(metaclass)

倾然丶 夕夏残阳落幕 提交于 2019-12-06 15:50:43
深刻理解Python中的元类(metaclass) 译注: 这是一篇在 Stack overflow 上很热的帖子。提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解。他知道这肯定和自省有关,但仍然觉得不太明白,希望大家可以给出一些实际的例子和代码片段以帮助理解,以及在什么情况下需要进行元编程。于是e-satis同学给出了神一般的回复,该回复获得了985点的赞同点数,更有人评论说这段回复应该加入到Python的官方文档中去。而e-satis同学本人在Stack Overflow中的声望积分也高达64271分。以下就是这篇精彩的回复(提示:非常长) 类也是对象 在理解元类之前,你需要先掌握Python中的类。Python中类的概念借鉴于Smalltalk,这显得有些奇特。在大多数 编程语言 中,类就是一组用来描述如何生成一个对象的代码段。在Python中这一点仍然成立: 1 2 3 4 5 6 >>>classObjectCreator(object): … pass … >>> my_object=ObjectCreator() >>>printmy_object <__main__.ObjectCreatorobjectat0x8974f2c> 但是,Python中的类还远不止如此。类同样也是一种对象。是的,没错,就是对象

how does metaclass work with the MRO list when super() is called?

孤街浪徒 提交于 2019-12-06 14:28:43
I'm really confused by the following code sample: class Meta_1(type): def __call__(cls, *a, **kw): # line 1 print("entering Meta_1.__call__()") print(cls) # line 4 print(cls.mro()) # line 5 print(super(Meta_1, cls).__self__) # line 6 rv = super(Meta_1, cls).__call__(*a, **kw) # line 7 print("exiting Meta_1.__call__()") return rv class Car(object, metaclass=Meta_1): def __new__(cls, *a, **kw): print("Car.__new__()") rv = super(Car, cls).__new__(cls, *a, **kw) return rv def __init__(self, *a, **kw): print("Car.__init__()") super(Car,self).__init__(*a, **kw) if __name__ == '__main__': c = Car()

In Objective C, how to obtain the metaclass object?

我怕爱的太早我们不能终老 提交于 2019-12-06 11:36:25
In Objective-C , how does one obtain the metaclass object? Both [self class] and [ClassName class] returns the Class object only. objc_getMetaClass Edit: Improved as per Greg's suggestion in the comments. object_getClass([Class class]); object_getClass([self class]) object_getClass([ClassName class]) You'll probably have to use the objective-C runtime function object_getClass to get the class of the class object. A good write up What is a meta-class in Objective-C goes through some detail on meta-classes. However, in some cases the class of the meta-class is the class itself. Do you wish to