metaclass

Metaclass not being called in subclasses

佐手、 提交于 2019-11-28 11:08:48
Here is a python session. >>> class Z(type): def __new__(cls, name, bases, attrs): print cls print name return type(name, bases, attrs) ... >>> class Y(object): __metaclass__ = Z ... <class '__main__.Z'> Y >>> class X(Y): ... pass ... >>> class W(Y): ... __metaclass__ = Z ... <class '__main__.Z'> W >>> After I define class X I expect Z._new__ to be called for it, and to print the two line, which is not happening, (as metaclass are inherited?) The problem is that the cls argument (which is the metaclass object) is not passed on when you call type , therefore the class object Y that is created

Error when calling the metaclass bases: function() argument 1 must be code, not str

自闭症网瘾萝莉.ら 提交于 2019-11-28 07:09:52
I tried to subclass threading.Condition earlier today but it didn't work out. Here is the output of the Python interpreter when I try to subclass the threading.Condition class: >>> import threading >>> class ThisWontWork(threading.Condition): ... pass ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str Can someone explain this error? Thanks! You're getting that exception because, despite its class-like name, threading.Condition is a function, and you cannot subclass functions. >

How does Inheritance work in Ruby?

本小妞迷上赌 提交于 2019-11-28 07:03:42
问题 According to Dave Thomas in his talk about the Ruby Object Model, there are no "class methods" in Ruby. There is only difference between whether the receiver of the method is a "class object" or an "instance object". class Dave def InstaceMethod ### will be stored in the current class (Dave) puts "Hi" end class << self ### Creates an eigenclass, if not created before def say_hello puts "Hello" end end end By default, ancestors method doesn't show the metaclass: class Dave class << self def

Custom placeholder like None in python

拥有回忆 提交于 2019-11-28 03:39:26
问题 I'm using argspec in a function that takes another function or method as the argument, and returns a tuple like this: (("arg1", obj1), ("arg2", obj2), ...) This means that the first argument to the passed function is arg1 and it has a default value of obj1, and so on. Here's the rub : if it has no default value, I need a placeholder value to signify this. I can't use None, because then I can't distinguish between no default value and default value is None . Same for False, 0, -1, etc. I could

Python3 Singleton metaclass method not working

自闭症网瘾萝莉.ら 提交于 2019-11-28 03:22:49
问题 I saw a lot of methods of making a singleton in Python and I tried to use the metaclass implementation with Python 3.2 (Windows), but it doesn"t seem to return the same instance of my singleton class. class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] class MyClass(object): __metaclass__ = Singleton a = MyClass() b = MyClass() print(a is b) #

Automatically setting an enum member's value to its name

妖精的绣舞 提交于 2019-11-28 00:44:40
I've been messing around with python's enum library and have come across a conundrum. In the docs, they show an example of an auto-numbering enum , wherein something is defined: class Color(AutoNumber): red = () green = () ... I want to make a similar class, but the value would automatically be set from the name of the member AND keep the functionality that you get from doing the str and enum mixin stuff So something like: class Animal(MagicStrEnum): horse = () dog = () Animal.dog == 'dog' # True I've looked at the source code of the enum module and tried a lot of variations messing around

Resolving metaclass conflicts

隐身守侯 提交于 2019-11-27 21:50:14
I need to create a class that uses a different base class depending on some condition. With some classes I get the infamous: TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases One example is sqlite3 , here is a short example you can even use in the interpreter: >>> import sqlite3 >>> x = type('x', (sqlite3,), {}) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Metaclass Mixin or Chaining?

早过忘川 提交于 2019-11-27 19:42:11
Is it possible to chain metaclasses? I have class Model which uses __metaclass__=ModelBase to process its namespace dict. I'm going to inherit from it and "bind" another metaclass so it won't shade the original one. First approach is to subclass class MyModelBase(ModelBase) : MyModel(Model): __metaclass__ = MyModelBase # inherits from `ModelBase` But is it possible just to chain them like mixins, without explicit subclassing? Something like class MyModel(Model): __metaclass__ = (MyMixin, super(Model).__metaclass__) ... or even better: create a MixIn that will use __metaclass__ from the direct

nose, unittest.TestCase and metaclass: auto-generated test_* methods not discovered

夙愿已清 提交于 2019-11-27 19:31:06
This is a follow-up question for unittest and metaclass: automatic test_* method generation : For this (fixed) unittest.TestCase layout: #!/usr/bin/env python import unittest class TestMaker(type): def __new__(cls, name, bases, attrs): callables = dict([ (meth_name, meth) for (meth_name, meth) in attrs.items() if meth_name.startswith('_test') ]) for meth_name, meth in callables.items(): assert callable(meth) _, _, testname = meth_name.partition('_test') # inject methods: test{testname}_v4,6(self) for suffix, arg in (('_false', False), ('_true', True)): testable_name = 'test{0}{1}'.format

Auto-register class methods using decorator

安稳与你 提交于 2019-11-27 18:50:52
I want to be able to create a python decorator that automatically "registers" class methods in a global repository (with some properties). Example code: class my_class(object): @register(prop1,prop2) def my_method( arg1,arg2 ): # method code here... @register(prop3,prop4) def my_other_method( arg1,arg2 ): # method code here... I want that when loading is done, somewhere there will be a dict containing: { "my_class.my_method" : ( prop1, prop2 ) "my_class.my_other_method" : ( prop3, prop4 ) } Is this possible? Not with just a decorator, no. But a metaclass can automatically work with a class