metaclass

Is there a way to set metaclass after the class definition?

北战南征 提交于 2019-11-30 06:57:03
问题 In order to set metaclass of a class, we use the __metaclass__ attribute. Metaclasses are used at the time the class is defined, so setting it explicitly after the class definition has no effect. This is what happens when I try to set metaclasses explicitly; >>> class MetaClass(type): def __new__(cls, name, bases, dct): dct["test_var"]=True return type.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): super(MetaClass, cls).__init__(name, bases, dct) >>> class A: __metaclass_

need to understand the flow of __init__, __new__ and __call__

☆樱花仙子☆ 提交于 2019-11-30 05:17:50
class Singleton(type): def __init__(self, *args, **kwargs): print 'calling __init__ of Singleton class', self print 'args: ', args print 'kwargs: ', kwargs super(Singleton, self).__init__(*args, **kwargs) self.__instance = None def __call__(self, *args, **kwargs): print 'running __call__ of Singleton', self print 'args: ', args print 'kwargs: ', kwargs, '\n\n' if self.__instance is None: self.__instance = super(Singleton, self).__call__(*args, **kwargs) return self.__instance class A(object): __metaclass__ = Singleton def __init__(self,a): print 'in __init__ of A: ', self self.a = a print

Java implementation - Meta classes

断了今生、忘了曾经 提交于 2019-11-30 05:03:55
The way I understand it, Java object model is 3 levels, each level describes the level beneath it, therefore there is one Meta class shared by all Classes (which are themselves objects?). My question is - how are constructors implemented in Java? (or any other class methods) my logic says that constructors should appear in the Meta classes, but since there is only one Meta class, it doesn't make any sense that it keeps all possible constructors, or is my understanding of this is all wrong.. In Java there's a single metaclass: the instances of the class Class are used to represent the types of

Metaclass multiple inheritance inconsistency

≯℡__Kan透↙ 提交于 2019-11-30 02:37:16
Why is this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyList(list, MyMixin): pass okay, and works as expected: created <class '__main__.MyMixin'> created <class '__main__.MyList'> But this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyObject(object, MyMixin): pass Is not okay, and blows up thusly?: created <class '__main__.MyMixin'> Traceback (most recent call last): File "/tmp/junk.py", line 11, in <module> class MyObject(object,

Where are methods defined at the ruby top level?

故事扮演 提交于 2019-11-30 02:27:39
At the top level, 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 top level ( self.meta is the eigenclass of main ): self.meta.private_instance_methods(false).include?(:hello) #=> true It appears that the hello method is simultaneously defined on the eigenclass of main as well as on Object . What's going on? Note that the false parameter to private_instance_methods excludes super

Python Metaclass : Understanding the 'with_metaclass()'

心已入冬 提交于 2019-11-29 22:49:47
I want to ask what the with_metaclass() call means in the definition of a class. E.g.: class Foo(with_metaclass(Cls1, Cls2)): Is it a special case where a class inherits from a metaclass? Is the new class a metaclass, too? with_metaclass() is a utility class factory function provided by the six library to make it easier to develop code for both Python 2 and 3. It uses a little slight of hand (see below) with a temporary metaclass, to attach a metaclass to a regular class in a way that's cross-compatible with both Python 2 and Python 3. Quoting from the documentation: Create a new class with

Is there any reason to choose __new__ over __init__ when defining a metaclass?

时间秒杀一切 提交于 2019-11-29 20:53:00
I've always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def __init__(self, name, bases, dict): #do stuff here Is there any reason to prefer one over the other? Update : Bear in mind that I'm asking about using __new__ and __init__ in a metaclass. I already understand the difference between them in another class. But in a metaclass, I can't use __new__ to implement caching because __new__ is only called upon class creation in a

Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.5

五迷三道 提交于 2019-11-29 20:45:53
I'd like to create a class which has abc.ABCMeta as a metaclass and is compatible both with Python 2.7 and Python 3.5. Until now, I only succeeded doing this either on 2.7 or on 3.5 - but never on both versions simultaneously. Could someone give me a hand? Python 2.7: import abc class SomeAbstractClass(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def do_something(self): pass Python 3.5: import abc class SomeAbstractClass(metaclass=abc.ABCMeta): @abc.abstractmethod def do_something(self): pass Testing If we run the following test using the suitable version of the Python interpreter

How does Django's Meta class work?

守給你的承諾、 提交于 2019-11-29 19:03:07
I am using Django which allows people to add extra parameters to a class by using class Meta . class FooModel(models.Model): ... class Meta: ... The only thing I found in Python's documentation was: class FooMetaClass(type): ... class FooClass: __metaclass__ = FooMetaClass However, I don't think this is the same thing. Tadeck You are asking a question about two different things: Meta inner class in Django models : This is just a class container with some options (metadata) attached to the model. It defines such things as available permissions, associated database table name, whether the model

Python, mixing PyQt5 and abc.ABCMeta

戏子无情 提交于 2019-11-29 18:11:07
I am trying to create an AbstractClass using both abc.ABCMeta and QObject as parents and cannot seems to make it work. Here is the Base class init. I have Pyqt5 and python 2.7 pyqtWrapperType = type(QObject) class ParamsHandler(abc.ABCMeta, pyqtWrapperType): def __init__(self, device_model, read_only=False): super(ParamsHandler, self).__init__() self.cmd_to_get_data = None self.device_model = device_model class ConfigParamsHandler(ParamsHandler): def __init__(self, device_model): super(ConfigParamsHandler, self).__init__(device_model) self.cmd_to_get_data = Commands.CONFIG_PARAMS I get a