Questions tagged [metaclass]

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.

1179 questions
-1
votes
1 answer

Magic methods on class self

I am looking for a way to call magic methods on class instances. In my case, I want to call hash on class based on properties. I found solution with metaclass, but I cannot access class properties from metaclass's method. class X(type): …
Patrik Valkovič
  • 706
  • 7
  • 26
-1
votes
1 answer

Metaclasses and methods

Adding a method to metaclass works perfectly in the below example. class Test(object): def __init__(self, x): self.x = x def double(self): return self.x*2 # method to add def quadruple(self): return self.x*4 #…
David Ross
  • 1,087
  • 1
  • 14
  • 21
-1
votes
2 answers

__metaclass__ not worked when create class with type("Test", (Base,), {'__metaclass__':Meta, ...})

cls = type("Test", (Base,), {"__metaclass__": Meta, "a": 1, ...}) I want to make some check for the attrs in the 3rd argument with Meta class, but this seem not worked, is there any other method?
wartalker
  • 338
  • 4
  • 11
-1
votes
1 answer

Implement _del_ method for a class with __getattribute__ overriden

Taking this question as a pointer, let's say there exists a class like the following: class Container(object): def __init__(self, **kwargs): self._meta = defaultdict(lambda: None) for attr, value in kwargs.iteritems(): …
viksit
  • 7,542
  • 9
  • 42
  • 54
-1
votes
1 answer

How does python create class using attributes in the parameter list of __new__() function?

I want to know how the __new__ function of type works, how can it create a class using the attr parameters. for Example: class ModelMetaclass(type): def __new__(cls, name, bases, attrs): return type.__new__(cls, name, bases,…
-1
votes
1 answer

GroovyCastException metaclass i18n

This question is connected with another. I'd like to add properties to constructor and overwrite getLocalisedMessage() function to get proper translated message with error. First I want to overload constructor to set properties, but when I…
Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
-1
votes
1 answer

practical example of defining using metaclass like setting it to ABCMeta or LoggingType

Example of setting metaclass to LoggingType i spotted at my workplace. import logging as _logging class SomeClass(object): __metaclass__ = _logging.LoggingType Here is the example I have seen for ABCMeta set to __metaclass__ I want to know the…
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
-1
votes
1 answer

How to build a hierarchical view of inherited classes in Python?

This is a question I tried to avoid several times, but I finally couldn't escape the subject on a recent project. I tried various solutions and decided to use one of them and would like to share it with you. Many solutions on internet simply don't…
dcexcal
  • 197
  • 7
-1
votes
1 answer

How to define dynamically, that some method of a class already exists in its base class? (Python 2.7)

I need to define either some method of a ChildClass overrides already existing method in BaseClass. (Python 2.7) class BaseClass(object): def method(self): pass class ChildClass1(BaseClass): def method(self): pass class…
efi
  • 123
  • 1
  • 7
-1
votes
1 answer

Objective-C dynamically define a method at runtime via a string

After reading several blog posts and searched through SO, I know how to add a method to a class, but no one touches on how to define the method body from a String. Here's an example blog post that gets close to what I want to…
maximus
  • 2,417
  • 5
  • 40
  • 56
-1
votes
2 answers

How to delete a class from globals right after creation?

I have a metaclass that just append a prefix to the classes on my modules. So far so good, the problem is that I want to remove the definition of the class that is calling the metaclass. For example if class A is prefixed to PrefixA I will end with…
Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68
-1
votes
1 answer

How can the shape of a pytables table column be defined by a variable?

I'm trying to create an IsDescription subclass, so that I can define the structure of a table I'm trying to create. One of the attributes of the subclass** needs to be shaped given a certain length that is unknown until runtime (it depends on a file…
darthbith
  • 18,484
  • 9
  • 60
  • 76
-1
votes
1 answer

type function on class with metaclass

class ParamMeta(type): def __str__(self): return self.__name__ class Param(object): __metaclass__=ParamMeta class SomeParam(Param): pass What I want is for: type(SomeParam)==Param How do I achieve this? Update: what do I need to…
jldupont
  • 93,734
  • 56
  • 203
  • 318
-1
votes
1 answer

How to implement MVC like this in Python?

For example, I have a config file named rule1.conf like this: [Basis] user = "sunhf" time = "2012-12-31" [Bowtie] path = "/usr/bin/bowtie" index = "/mnt/Storage/sync/hg19" And a models.py like this(using a package named magic.py..): from magic…
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
-2
votes
1 answer

Why do I get an Error when creating an instanz of a class that has a metaclass

class Meta(type): def __new__(cls, class_name, bases, attrs): a={} for name, val in attrs.items(): if name.startswith("__"): a[name] = val else: a[name.upper()] = val …
Fuorb
  • 27
  • 6
1 2 3
78
79