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
0
votes
1 answer

Replace Python List/Set implementation with custom one

I currently write a source-to-source compiler from a language developed by a professor of mine to Python. It goes all well, but some of the language idiosyncrasies drive me mad. The source language has 1-indexed lists and different slicing rules,…
jcklie
  • 4,054
  • 3
  • 24
  • 42
0
votes
1 answer

Dynamically assign at class creation a dynamically created metaclass

I believe the question sounds a bit confusing, so I'll give more details in the following. I have these two classes define, one of which inherits type: class ProductType(type): def __new__(cls, name, bases, attrs): return…
linkyndy
  • 17,038
  • 20
  • 114
  • 194
0
votes
2 answers

Python class specific global attributes

Lets say I have class SuperModel, Model, and SubModel. I want to define a global attribute 'collection' that is the name of the class. For example, If I do: Model.collection it will return 'Model'. If I subclass Model, say class…
kleesc
  • 3
  • 2
0
votes
0 answers

Is it dangerous to define __contains__ on a metaclass?

I'm writing a custom EnumMeta class in Python 2.7 that will collect enum keys and values from some class and augment that class with some additional fields. class EnumMeta(type): def __init__(cls, name, bases, props): cls.__all_values__…
LavaScornedOven
  • 737
  • 1
  • 11
  • 23
0
votes
2 answers

Ruby trying to programatically avoid multiple definitions in subclasses

Quiet new to ruby I can't figure out something. Here's a Sample code class Big def self.metaclass; class << self; self; end; end def self.convertor b metaclass.instance_eval do define_method( :convert ) do |val| return b val …
zedryas
  • 718
  • 6
  • 19
0
votes
1 answer

Division of metatable

got some problem with metatable. This is my simple metatable: local mt = {} function mt:add(n) return setmetatable({n = n}, {__index = mt}) end function mt:get() return self.n end Now I want to add some division…
user3225882
0
votes
1 answer

What happened in the Django 1.6 branch that affected how Manager metaclasses work?

I have a little utility module, django-delegate, that lets you define methods on a QuerySet subclass and then “delegate” those method definitions to a corresponding Manager subclass, by dint of a @delegate decoration. It looks like this, if I can…
fish2000
  • 4,289
  • 2
  • 37
  • 76
0
votes
1 answer

How to correctly inherit from class that returns other class based on condition?

I will first write the code and than the full explanation of what I am trying to achieve as it is easier this way: global child_selector class Base(object): def __init__(self): self.extended_name = self.name + '_Base' class Child1(Base): …
skamsie
  • 2,614
  • 5
  • 36
  • 48
0
votes
1 answer

add Closure method to metaClass of groovy class

i knewt how to add method to metaClass of java.util.List : java.util.List.metaClass.average={ return delegate.sum()/delegate.size(); } and if i have def myList=[new Person(score:1),new Person(score:2)] , i call to new method as following:…
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
0
votes
0 answers

General way to add a save capability to a class in Python

I recently hit on the idea of using some form of metaprogramming to replace the (admittedly limit) boilerplate code needed to persist and then load objects using pickle. So you'd get something like class MyClass(object): # Something to add…
kuzzooroo
  • 6,788
  • 11
  • 46
  • 84
0
votes
2 answers

Get the name of variable with which a method was approached from this class in Ruby

I want to get the name of variable with which class was approached IN the method of the class. Like that: class Object def my_val "#{???} = '#{self.to_s}'" end end a1 = "val1" a1.my_val # = a1 = 'val1' a2 = "val2" a2.my_val # = a2 = 'val2'…
Tolsi
  • 656
  • 9
  • 20
0
votes
0 answers

Distinguish abstract base class from implementation

Developing a metaclass that provides self-registration of subclasses (somewhat similar to this idea), I came across the following problem: Given a class that may be either an abstract base class or an implementation of one, how do I distinguish…
Emilia Bopp
  • 866
  • 10
  • 20
0
votes
1 answer

Create Multiple File Upload Metabox in Wordpress

I am trying to create a metabox to upload multiple files (can be images or files). Currently I have the upload field and its working file till the time we press upload and saving the data. The only problem is displaying the files. Below is my code,…
Himanshu Jain
  • 444
  • 1
  • 8
  • 23
0
votes
1 answer

Ruby's equivalent of Groovy's metaclass.method_name

I have a method where symbol_price is a hash with keys as symbols and price as values. def get_price_for_symbol(symbol_price, symbol) symbol_price[symbol] end In Groovy if we want to mock out the main class method get_price_for_symbol we write in…
0
votes
0 answers

Alternative Django model creation

I am trying to write an alternative way of creating models, other from the one Django uses (metaclasses that is). Currently, Django uses a metaclass to put all field declarations from a user-declared model in a fields dictionary and then uses this…
linkyndy
  • 17,038
  • 20
  • 114
  • 194