metaclass

Groovy metaclass

青春壹個敷衍的年華 提交于 2019-12-24 17:07:48
问题 I have an existing class that extends a Number & implements Comparable, and I have been using this class extensively - including in my test classes (Junit & Spock). I have also been comparing instances of this class using == operator in the test classes. As I understand it, Groovy will not use .equals() for comparison of classes which implements Comparable. Furthermore, after trawling Groovy code, I also found out that my class will be compared using Integer (hence losing decimal points, etc)

Avoid inheriting generated class attributes using metaclass

爷,独闯天下 提交于 2019-12-24 09:37:53
问题 I was thinking of automatically adding child classes to parent for "chaining" using a metaclass. However, inheriting these attributes from parent classes messes thing up. Is there a nice way to avoid this? class MetaError(type): def __init__(cls, name, bases, attrs): for base in bases: setattr(base, name, cls) super(MetaError, cls).__init__(name, bases, attrs) class BaseError(Exception, object): def __init__(self, message): super(BaseError, self).__init__(message) class HttpError(BaseError):

Grails and Groovy metaclass package name convention

◇◆丶佛笑我妖孽 提交于 2019-12-24 02:42:52
问题 I would like to be able to place my metaclasses in the package groovy.runtime.metaclass according to the convention defined here using the delegating meta class. I have placed MyClassMetaClass in groovy.runtime.metaclass.mypackage. for the concrete class mypackage.MyClass{} . Unfortunately calls to MyClass{} are not intercepted by MyClassMetaClass{} . Perhaps this is related to how grails initializes..? 回答1: It is possible to use a delegating meta class in a grails project by changing

Is there a way to run a method automatically on the initialization of an instance without using __init__?

ぐ巨炮叔叔 提交于 2019-12-24 02:33:37
问题 I am writing some unit tests with Pytest. If I want them to be collected automatically, I have to avoid the __init__ constructor. (If there's a way to make Pytest collect tests with the __init__ constructor I'd take that as an alternate useful answer.) My unit tests have some variables and methods in common. Right now I have base test class TestFoo, and child test class TestBar(TestFoo), and grandchild test class TestBaz(TestBar). Since I can't have an init method, right now I'm calling a

__call__ method of type class

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 02:09:19
问题 From all I understand about Python object oriented programming if a class has __call__ method defined that would be invoked if we use the instance of the class like a function call. For example: class Employee: def __init__(self,name,sal): self.name = name self.salary = sal def __call__(self,value): return self.salary * value e = Employee("Subhayan",20000) print (e(10)) So the __call__ method takes the object instance as the first argument. I am just trying to understand metaclasses in Python

Overriding private method with metaClass in Groovy

扶醉桌前 提交于 2019-12-23 18:17:47
问题 This snippet used to work properly with Groovy 2.1.0: class User { private String sayHello() { return "hello" } } assert new User().sayHello() == "hello" User.metaClass.sayHello = { return "goodbye" } assert new User().sayHello() == "goodbye" but it does not work anymore in Groovy 2.4.3. Does anybody know how to override the behaviour of a private method with Groovy (if possible)? 来源: https://stackoverflow.com/questions/31938551/overriding-private-method-with-metaclass-in-groovy

Is it possible to dynamically create a metaclass for a class with several bases, in Python 3?

假装没事ソ 提交于 2019-12-23 17:05:15
问题 In Python 2, with a trick it is possible to create a class with several bases, although the bases have metaclasses that are not subclass of each other. The trick is that these metaclasses have themselves a metaclass (name it a "metametaclass"), and this metametaclass provides the metaclasses with a call method that dynamically creates a common sub-metaclass of the base metaclasses, if necessary. Eventually, a class results whose metaclass is the new sub-metaclass. Here is the code: >>> class

python当中__metaclass__探讨

醉酒当歌 提交于 2019-12-23 16:17:52
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最初博主是希望在python当中创建一个单列模式的类,因为python当中不像java和php当中有权限修饰符(private),所以实现起来要绕一点。 网上找了一下python实现单列模式,类似的大概有这种方法: class singleton(type): """ 实现单列模式的元类 总之,metaclass的主要任务是: 拦截类, 修改类, 返回类 """ def __init__(cls,classname,parrentstuple,attrdict): """ """ super(SigleInstance,cls).__init__(classname,parrentstuple,attrdict) cls._instance = None def __call__(self,*args,**kargs): """ """ if self._instance: return self._instance else: self._instance = super(SigleInstance,self).__call__(*args,**kargs) return self._instance 这就是单列的元类,我把它小写了,因为type也是小写的。然后呢,在即将要实现单列的class当中这样写:

Deleting existing class variable yield AttributeError

岁酱吖の 提交于 2019-12-23 15:19:06
问题 I am manipulating the creation of classes via Python's metaclasses. However, although a class has a attribute thanks to its parent, I can not delete it. class Meta(type): def __init__(cls, name, bases, dct): super().__init__(name, bases, dct) if hasattr(cls, "x"): print(cls.__name__, "has x, deleting") delattr(cls, "x") else: print(cls.__name__, "has no x, creating") cls.x = 13 class A(metaclass=Meta): pass class B(A): pass The execution of the above code yield an AttributeError when class B

How to get attributes in the order they are declared in a Python class?

笑着哭i 提交于 2019-12-23 10:03:27
问题 As described in PEP435, an enum can be defined this way: class Color(Enum): red = 1 green = 2 blue = 3 And the resulting enum members of Color can be iterated in definition order: Color.red, Color.green, Color.blue . This reminds me of Form in Django , in which fields can be rendered in the order they are declared in a Form subclass. They implemented this by maintaining a field counter, every time a new field is instantiated the counter value get incremented. But in the definition of Color ,