metaclass

How to add method using metaclass

How do I add an instance method to a class using a metaclass (yes I do need to use a metaclass)? The following kind of works, but the func_name will still be "foo": def bar(self): print "bar" class MetaFoo(type): __new__(cls, name, bases, dict): dict["foobar"] = bar return type(name, bases, dict) class Foo(obje...

Using the docstring from one method to automatically overwrite that of another method.

The problem: I have a class which contains a template method "execute" which calls another method "_execute". Subclasses are supposed to overwrite "_execute" to implement some specific functionality. This functionality should naturally be documented in the docstring of "_execute". I also expect users to create their own subclasses to ex...

What is a metaclass in Python?

I´ve mastered almost all the Python concepts (well, let´s say there are just OO concepts :-)) but this one is tricky. I know it has something to do with introspection but it´s still unclear to me. So what are metaclasses? What do you use them for? Concrete examples, including snippets, much appreciated! ...

How to override HTTP request verb in GAE

In the context of a Google App Engine Webapp framework application: I want to changed the request verb of a request in the case a parameter _method is provided, for example if a POST request comes in with a parameter _method=PUT, I need to change the request to call the put method of the handler. This is to cope with the way prototype.j...

What are your (concrete) use-cases for metaclasses in Python?

I have a friend who likes to use metaclasses, and regularly offers them as a solution. I am of the mind that you almost never need to use metaclasses. Why? because I figure if you are doing something like that to a class, you should probably be doing it to an object. And a small redesign/refactor is in order. Being able to use metaclas...

"MetaClass", "__new__", "cls" and "super" - can someone explain the mechanism exactly

I have read posts like these: What is a metaclass in Python? What are your (concrete) use-cases for metaclasses in Python? Python's Super is nifty, but you can't use it but somehow I got confused, many confusions like when and why i would have to do something like this #refer link1 return super(MyType, cls).__new__(cls, name, bas...

Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?

I recently discovered metaclasses in python. Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritance works, etc. All of this be...

Groovy: Delegating metaclass for an Interface?

Using Groovy's package name convention, I can intercept Groovy method calls to a Java method like so: package groovy.runtime.metaclass.org.myGang.myPackage class FooMetaClass extends groovy.lang.DelegatingMetaClass { StringMetaClass(MetaClass delegate) { super(delegate); } public Object getProperty(Object a, St...

How do I reference the GroovyObject instance from MetaClass methods in Groovy?

This is a contrived example of what I want to do, but minimally expresses the behavior desired. I want to reference the instance of the object on which the property access is being invoked. I tried 'this' first, but that refers to the enclosing class rather than either the MetaClass or the String instance. String.metaClass.propertyMi...

UML 2 Profiles Package: How to extend Operation?

Hi, guys! I'm in a big trouble with uml profile implementation. The problem is I can't get how can I extend uml Operation class from Infrastructure::Core::Constructs using Profile? The Extension association from Profiles package allow metaclass only to be of type Core::Constructs::Class according to uml metamodel. Is Operation a met...

Python metaclasses

I've been hacking classes in python like this : def hack(f,aClass) : class MyClass(aClass) : def f(self) : f() return MyClass A = hack(afunc,A) Which looks pretty clean to me. It takes a class A, creates a new class derived from it, that has an extra method, calling f, and then reassigns the new clas to A. How does t...

How can I implement metaclasses in C++?

I've been reading a bit about what metaclasses are, but I would like to know if they can be achieved in C++. I know that Qt library is using MetaObjects, but it uses an extension of C++ to achieve it. I want to know if it is possible directly in C++. Thanks. ...

callable as instancemethod?

Let's say we've got a metaclass CallableWrappingMeta which walks the body of a new class, wrapping its methods with a class, InstanceMethodWrapper: import types class CallableWrappingMeta(type): def __new__(mcls, name, bases, cls_dict): for k, v in cls_dict.iteritems(): if isinstance(v, types.FunctionType): ...

Shouldn't __metaclass__ force the use of a metaclass in Python?

I've been trying to learn about metaclasses in Python. I get the main idea, but I can't seem to activate the mechanism. As I understand it, you can specify M to be as the metaclass when constructing a class K by setting __metaclass__ to M at the global or class level. To test this out, I wrote the following program: p = print class M(t...

Grails behavior difference between run-app and run-war

I'm relatively new to Groovy and Grails and am trying them out in my spare time. I've got a small test Grails application that I'm able to run fine using grails run-app, but grails run-war results in an error. In the grails-app/conf/BootStrip.init method, I'm adding some property getters onto the DefaultGrailsControllerClass and Defaul...

Reverse mapping class attributes to classes in Python

Hi all, I have some code in Python where I'll have a bunch of classes, each of which will have an attribute _internal_attribute. I would like to be able to generate a mapping of those attributes to the original class. Essentially I would like to be able to do this: class A(object): _internal_attribute = 'A attribute' class B(object...

Groovy: adding methods to instances and classes with metaClass doesn't work?

See the code below. Old instances of a class created before a method is added to the class using metaClass should not understand the method right? The assert statement below the 'PROBLEMATIC LINE' comment is executed when I think it should not be, as the old parentDir instance should not understand the blech() message. // derived from...

How to add a new closure to a class in groovy.

From Snipplr Ok here is the script code, in the comments is the question and the exception thrown class Class1 { def closure = { println this.class.name println delegate.class.name def nestedClos = { println owner.class.name } nestedClos() } } def clos = new Class1().closure ...

How does a classmethod object work?

I'm having trouble to understand how a classmethod object works in Python, especially in the context of metaclasses and in __new__. In my special case I would like to get the name of a classmethod member, when I iterate through the members that were given to __new__. For normal methods the name is simply stored in a __name__ attribute, ...

Where are methods defined at the ruby top level?

EDIT: this post only applies to Ruby 1.9 Hi, At the toplevel method definition should result in private methods on Object, and tests seem to bear this out: def hello; "hello world"; end Object.private_instance_methods.include?(:hello) #=> true Object.new.send(:hello) #=> "hello world" However, the following also works (at toplevel)...