metaclass

Implementing Singleton as metaclass, but for abstract classes

雨燕双飞 提交于 2019-12-13 02:35:13
问题 I have an abstract class and I would like to implement Singleton pattern for all classes that inherit from my abstract class. I know that my code won't work because there will be metaclass attribute conflict. Any ideas how to solve this? from abc import ABCMeta, abstractmethod, abstractproperty class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls]

Scope of Groovy's metaClass?

老子叫甜甜 提交于 2019-12-12 16:03:29
问题 I have an application which can run scripts to automate certain tasks. I'd like to use meta programming in these scripts to optimize code size and readability. So instead of: try { def res = factory.allocate(); ... do something with res ... } finally { res.close() } I'd like to Factory.metaClass.withResource = { c -> try { def res = factory.allocate(); c(res) } finally { res.close() } } so the script writers can write: factory.withResource { res -> ... do something with res ... } (and I could

How to register implementation of abc.MutableMapping as a dict subclass?

ぐ巨炮叔叔 提交于 2019-12-12 15:52:13
问题 I would like my SpreadSheet class below to be considered a dict subclass by the isinstance() built-in, but when I try to register it as such, an AttributeError Exception is thrown (also shown below). What is a (or the) way to do something like this? Note: My question is similar to Is it possible to be a virtual subclass of a built in type?, but its accepted answer doesn't address the titular question asked (so please don't vote to close this as a duplicate). The primary motivation for wanting

deepcopy does not respect metaclass

旧街凉风 提交于 2019-12-12 14:02:51
问题 I have a class which, by design, must follow the singleton pattern. So I went ahead and implemented it using a metaclass . Everything worked nicely until a bug was reported which, in summary, said that deepcopy -ied instances of my singleton class were not the same instances. I can get around this bug by inheriting from a base singleton -type class, but I'd rather not, for reasons pointed out in this question. A working example of this issue is presented below: class SingletonMeta(type): def

python - how to get a list of inner classes?

二次信任 提交于 2019-12-12 12:29:10
问题 I'm writing a small Python application that contains a few nested classes, like the example below: class SuperBar(object): pass class Foo(object): NAME = 'this is foo' class Bar(SuperBar): MSG = 'this is how Bar handle stuff' class AnotherBar(SuperBar): MSG = 'this is how Another Bar handle stuff' I'm using nested classes to create some sort of hierarchy and to provide a clean way to implement features for a parser. At some point, I want to create a list of the inner classes. I'd like to have

In Objective C, how to obtain the metaclass object?

。_饼干妹妹 提交于 2019-12-12 10:12:38
问题 In Objective-C , how does one obtain the metaclass object? Both [self class] and [ClassName class] returns the Class object only. 回答1: objc_getMetaClass Edit: Improved as per Greg's suggestion in the comments. object_getClass([Class class]); 回答2: object_getClass([self class]) object_getClass([ClassName class]) 回答3: 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

Python metaclass and the object base class

喜欢而已 提交于 2019-12-12 08:53:39
问题 After reading the excellent SO post, I tried crafting a module level metaclass: def metaclass(future_class_name, future_class_parents, future_class_attrs): print "module.__metaclass__" future_class_attrs["bar"]="bar" return type(future_class_name, future_class_parents, future_class_attrs) __metaclass__=metaclass class Foo(object): def __init__(self): print 'Foo.__init__' f=Foo() This doesn't work (i.e. "module. metaclass " doesn't get printed) unless I remove the object base class of Foo. How

Is there any reason to choose __new__ over __init__ when defining a metaclass?

送分小仙女□ 提交于 2019-12-12 07:08:57
问题 I've always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def __init__(self, name, bases, dict): #do stuff here Is there any reason to prefer one over the other? Update : Bear in mind that I'm asking about using __new__ and __init__ in a metaclass. I already understand the difference between them in another class. But in a metaclass

Grails: println only works sometimes or something

我与影子孤独终老i 提交于 2019-12-12 06:03:17
问题 I make a brand new grails project and put this in the bootstrap: ExpandoMetaClass.enableGlobally() Integer.metaClass.precision = {->return 1} println 3.precision() println "rofl" println 15.precision() And it does what I expect, run-app prints: 1 rofl 1 But if i take out the println "rofl" it won't print that second one. It just prints one 1 without the rofl... WTF? Again, becasue this doesn't make any sense to me, this code: ExpandoMetaClass.enableGlobally() Integer.metaClass.precision = {-

Understanding metaclasses in Python

可紊 提交于 2019-12-12 04:21:18
问题 I am trying to get my head around metaclass but i still can't really get the concept of it. For all i know: Any class is itself an instance of type "type" - therefore "calling" a class just calls the method __call__ on its class - which happens to be type's __call__ . The effect of type.__call__ is exactly: on code like: class A: pass b = A() The sequence of steps i know here is: 1. type.__call__ receives the class A itself as its first parameter. It calls the A.__new__ - in pseudocode we