metaclass

Understanding metaclass and inheritance in Python [duplicate]

天大地大妈咪最大 提交于 2019-12-02 17:50:35
This question already has an answer here: What are metaclasses in Python? 16 answers I have some confusion regarding meta-classes. With inheritance class AttributeInitType(object): def __init__(self,**kwargs): for name, value in kwargs.items(): setattr(self, name, value) class Car(AttributeInitType): def __init__(self,**kwargs): super(Car, self).__init__(**kwargs) @property def description(self): return "%s %s %s %s" % (self.color, self.year, self.make, self.model) c = Car(make='Toyota', model='Prius', year=2005, color='green') print c.description With meta class class AttributeInitType(type):

Customary To Inherit Metaclasses From type?

非 Y 不嫁゛ 提交于 2019-12-02 16:16:11
I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass like def metacls(clsName, bases, atts): .... return type(clsName, bases, atts) However, I have seen a lot of people write their metaclasses in the following way: class Metacls(type): def __new__(meta, clsName, bases, atts): .... return type.__new__(meta, clsName, bases, atts) As far as I can see, these would both do the same thing. Is there any reason to use the base class instead? Is it customary?

What are Python metaclasses useful for?

让人想犯罪 __ 提交于 2019-12-02 16:16:00
What can be done with metaclasses that can't be in any other way? Alex Martelli told that there are tasks that can't be achieved without metaclasses here Python metaclasses vs class decorators I'd like to know which are? Metaclasses are indispensable if you want to have class objects (as opposed to instances of class objects) equipped with "special customized behavior", since an object's behavior depends on special methods on the type of the object, and a class object's type is, exactly a synonym for, the metaclass. For example, if you want a class object X such that "print X" emits "Time is

Python metaclasses vs class decorators

 ̄綄美尐妖づ 提交于 2019-12-02 14:29:24
What are the main differences between Python metaclasses and class decorators? Is there something I can do with one but not with the other? Decorators are much, much simpler and more limited -- and therefore should be preferred whenever the desired effect can be achieved with either a metaclass or a class decorator. Anything you can do with a class decorator, you can of course do with a custom metaclass (just apply the functionality of the "decorator function", i.e., the one that takes a class object and modifies it, in the course of the metaclass's __new__ or __init__ that make the class

“MetaClass”, “__new__”, “cls” and “super” - what is the mechanism exactly?

假装没事ソ 提交于 2019-12-02 13:59:45
I have read posts like these: What is a metaclass in Python? What are your (concrete) use-cases for metaclasses in Python? Python's Super is nifty, but you can't use it But somehow I got confused. Many confusions like: When and why would I have to do something like the following? # Refer link1 return super(MyType, cls).__new__(cls, name, bases, newattrs) or # Refer link2 return super(MetaSingleton, cls).__call__(*args, **kw) or # Refer link2 return type(self.__name__ + other.__name__, (self, other), {}) How does super work exactly? What is class registry and unregistry in link1 and how exactly

How do I make a new Moose class and instantiate an object of that class at runtime?

て烟熏妆下的殇ゞ 提交于 2019-12-02 12:38:37
问题 After creating a metaclass using Moose::Meta::Class->create , how do I instantiate a real Moose class with that class as a metaclass? (I need to create the metaclass also because I also want to apply some roles to it.) 回答1: The metaclass is the class, of course. If you want an instance of that class, just do: my $instance = $meta->name->new You might also need to make sure that $meta doesn't get collected too soon. Generally, you do this: $meta->add_method( meta => sub { $meta } ); That will

Why are the lookup procedures for getting an attribute from a class and from an instance different?

百般思念 提交于 2019-12-02 09:06:24
Python in a Nutshell describes the lookup procedures when getting an attribute. The book distinguishes two cases the lookup procedure when getting an attribute from a class, e.g. cls.name Getting an attribute from a class When you use the syntax C.name to refer to an attribute on a class object C , the lookup proceeds in two steps: When name is a key in C.__dict__ , C.name fetches the value v from C.__dict__['name'] . Then, when v is a descriptor (i.e., type(v) supplies a method named __get__ ), the value of C.name is the result of calling type(v).__get__(v, None, C) . When v is not a

how does IPython's ? operator actually work?

久未见 提交于 2019-12-02 02:17:37
问题 So i was thinking that in order to implement such a feature in a console application , where appending a question mark at the end of a function name will pour out it's doc string , i would have probably used a feature like metaclasses , where upon definition/import , i'd duplicate all the module member names and produce new ones just for typing out doc strings. Then i noticed that you don't need actual parenthesis to call the helper functions and python doesn't actually allow you to put a

how does IPython's ? operator actually work?

不打扰是莪最后的温柔 提交于 2019-12-02 00:02:42
So i was thinking that in order to implement such a feature in a console application , where appending a question mark at the end of a function name will pour out it's doc string , i would have probably used a feature like metaclasses , where upon definition/import , i'd duplicate all the module member names and produce new ones just for typing out doc strings. Then i noticed that you don't need actual parenthesis to call the helper functions and python doesn't actually allow you to put a question mark at the end of the function name anyway.... So is this done in python or am i just wasting my

subclass __module__ set to metaclass module when manually creating new class with type()

て烟熏妆下的殇ゞ 提交于 2019-12-01 19:28:59
问题 In the following example, the newly created subclass ends up being the metaclass __module__ rather than the parent classes' module. I've only seen this happen when using ABCMeta so it could be something specific to that module, anyone know what might be happening? In [1]: from abc import ABCMeta In [2]: class test(metaclass=ABCMeta): ...: pass ...: In [3]: newclass = type('newclass', (test,), {}) In [4]: newclass.__module__ Out[4]: 'abc' The behavior I want happens when I define the subclass