In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses.
Questions tagged [metaclass]
1179 questions
0
votes
3 answers
Design choice for two almost identical classes
I have two classes Boat and Mines, which have exactly the same methods and variables.
the only difference is that they are Initialized in different positions.
for Boat
xPosition = 3
yPosition = 4
for Mine
xPosition = 1
yPosition = 1
I've been told…

Koborl
- 235
- 1
- 11
0
votes
2 answers
Groovy: Metaclass - accessing instance methods
I am trying to access a method of an instance using the metaclass, but I get the error that the property does not exist. Is there a way to access properties of classes declared in another class.
Here is a contrived example:
class DogFood {
def ft…

chandra
- 67
- 1
- 10
0
votes
2 answers
Create current class default instance in its base class
Preamble: I have objects, some of them could be created by default constructor and left without modifications, so such objects could be considered as "empty". Sometimes I need to verify whether some object is "empty" or not. It could be done in the…

rook
- 5,880
- 4
- 39
- 51
0
votes
2 answers
python dict of class objects that inherits attributes from class objects
How can I create a dictionary class 'D' that stores instances of some class 'A' and inherits wrapped versions of the methods of 'A'?
For example, suppose a = A() and a.first() is a function that does something to 'a' and returns some object. Then I…

mathtick
- 6,487
- 13
- 56
- 101
0
votes
3 answers
How does eclipse's pydev do code completion?
Does anyone know how pydev determines what to use for code completion? I'm trying to define a set of classes specifically to enable code completion. I've tried using __new__ to set __dict__ and also __slots__, but neither seems to get listed in…

Brett Stottlemyer
- 2,734
- 4
- 26
- 38
0
votes
2 answers
How to call object method for any object in my metaclass?
I have basically this object structure:
TJSONStructure = class(TObject);
TReqBase = class(TJSONStructure)
private
token: Int64;
public
procedure FillWithTemplateData; virtual;
end;
TReqLogin = class(TReqBase)
private
username,
…

Jan Doggen
- 8,799
- 13
- 70
- 144
0
votes
1 answer
What is the automatic delegate of a groovy closure?
In the last example of this page (http://groovy.codehaus.org/JN3525-MetaClasses), the closure code to override the metaClass invokeMethod call refers to a "delegate". Code is also copied below:
class Bird{
def name= 'Tweety'
def twirp(){ 'i…

ZenBalance
- 10,087
- 14
- 44
- 44
0
votes
2 answers
Python metaclass, execfile, inspect, globals(), namespace
When the following script is run directly, it operates as anticipated:
import inspect
__module__ = "__main__"
__file__ = "classes.py"
test_str = "test"
class met(type):
def __init__(cls, name, bases, dct):
setattr(cls, "source",…

dilbert
- 3,008
- 1
- 25
- 34
0
votes
1 answer
How does Django handle foreignKeys internally?
I am curious how Django handles model relationships at the object level because I am working on building a custom json serializer, and I need to understand this so I have properly handle nested serialization. I am almost positive I will have to dive…

user1876508
- 12,864
- 21
- 68
- 105
0
votes
1 answer
metaclassing, defining __init__ to pass attributes for the instances (not for the class) using a dictionary
Similarly to this question I would like to pass the attributes from a dictionary to an instance of a class, but creating the class using type, like this:
attrs = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }
e = type('Employee',…

Saullo G. P. Castro
- 56,802
- 26
- 179
- 234
0
votes
1 answer
How can I fix this metaclass-borne TypeError?
I'm trying to create a simple dynamic plugin system in which plugins inheriting from class Bot automatically register themselves on import.
Below is my code thus far. Please note that I am working from this short article.
import os.path as osp
from…

Louis Thibault
- 20,240
- 25
- 83
- 152
0
votes
1 answer
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…

jjchiw
- 4,375
- 1
- 29
- 30
0
votes
1 answer
Groovy respondsTo no argument method
I have an object, MyObject, that I need to check if it contains a method, say format. I need to check if this method signature has no arguments, or if it has an argument of MyType. I don't see a way of checking if a method respondsTo no arguments.…

Joseph
- 1,442
- 21
- 44
0
votes
3 answers
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,…

awyatt
- 51
- 3
0
votes
1 answer
Creating a simple metaclass
I asked a question earlier on today, where I only wanted a solution for my problem.
I was expecting the use of metaclasses, but got something different and it worked.
However, now I'm interested in something related to my earlier question: Why…
user2032433