metaclass

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

限于喜欢 提交于 2019-12-01 18:29:23
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 in the more standard way: In [5]: class subtest(test): ...: pass ...: In [6]: subtest.__module__ Out[6]

Shouldn't __metaclass__ force the use of a metaclass in Python?

自古美人都是妖i 提交于 2019-12-01 17:19:10
I've been trying to learn about metaclasses in Python. I get the main idea, but I can't seem to activate the mechanism. As I understand it, you can specify M to be as the metaclass when constructing a class K by setting __metaclass__ to M at the global or class level. To test this out, I wrote the following program: p = print class M(type): def __init__(*args): type.__init__(*args) print("The rain in Spain") p(1) class ClassMeta: __metaclass__ = M p(2) __metaclass__ = M class GlobalMeta: pass p(3) M('NotMeta2', (), {}) p(4) However, when I run it, I get the following output: C:\Documents and

Shouldn't __metaclass__ force the use of a metaclass in Python?

坚强是说给别人听的谎言 提交于 2019-12-01 17:02:04
问题 I've been trying to learn about metaclasses in Python. I get the main idea, but I can't seem to activate the mechanism. As I understand it, you can specify M to be as the metaclass when constructing a class K by setting __metaclass__ to M at the global or class level. To test this out, I wrote the following program: p = print class M(type): def __init__(*args): type.__init__(*args) print("The rain in Spain") p(1) class ClassMeta: __metaclass__ = M p(2) __metaclass__ = M class GlobalMeta: pass

How does the Objective-C runtime instantiate the root metaclass and other class descriptions?

帅比萌擦擦* 提交于 2019-12-01 16:50:51
I'm trying to implement a basic object-oriented ANSI C runtime and using Objective-C as a guide. They're seems to be three parts. A Class Description, Class Interface, and Class Implementation. In order for the Class Interface to be instantiated, the familiar method of using the Class object to instantiate one's object can only happen if the runtime has already instantiated your class object using the class description. So are all Class definitions allocated statically at first run to provide the ability to instantiate using the Class object? Or if they are allocated dynamically (on initial

Can I define a __repr__ for a class rather than an instance? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-01 15:28:21
This question already has an answer here: How to create a custom string representation for a class object? 5 answers Can I define a __repr__ for a class rather than an instance? For example, I'm trying to do this class A(object): @classmethod def __repr__(cls): return 'My class %s' % cls What I get is In [58]: a=A() In [59]: a Out[59]: My class <class '__main__.A'> In [60]: A Out[60]: __main__.A I'm trying to get the output of line 60 to look like "My Class A", not for the instance a. The reason I want to do this is I'm generating a lot of classes using Python's metaclass. And I want a more

How does the Objective-C runtime instantiate the root metaclass and other class descriptions?

社会主义新天地 提交于 2019-12-01 15:27:54
问题 I'm trying to implement a basic object-oriented ANSI C runtime and using Objective-C as a guide. They're seems to be three parts. A Class Description, Class Interface, and Class Implementation. In order for the Class Interface to be instantiated, the familiar method of using the Class object to instantiate one's object can only happen if the runtime has already instantiated your class object using the class description. So are all Class definitions allocated statically at first run to provide

Groovy instance.metaclass vs this.metaclass

*爱你&永不变心* 提交于 2019-12-01 13:28:50
I have a following script: task myTask {} class Person { Person() { Person instance = this println this.metaClass.class.name println this.getMetaClass().class.name println instance.metaClass.class.name println instance.getMetaClass().class.name } } Person person = new Person() And the output is : groovy.lang.MetaClassImpl groovy.lang.MetaClassImpl org.codehaus.groovy.runtime.HandleMetaClass org.codehaus.groovy.runtime.HandleMetaClass Can anyone explain to me what is going on? Thanks in advance. Take a look at this class , class Person { def a, b Person() { a = this b = this println "this $this

Groovy instance.metaclass vs this.metaclass

风流意气都作罢 提交于 2019-12-01 11:35:15
问题 I have a following script: task myTask {} class Person { Person() { Person instance = this println this.metaClass.class.name println this.getMetaClass().class.name println instance.metaClass.class.name println instance.getMetaClass().class.name } } Person person = new Person() And the output is : groovy.lang.MetaClassImpl groovy.lang.MetaClassImpl org.codehaus.groovy.runtime.HandleMetaClass org.codehaus.groovy.runtime.HandleMetaClass Can anyone explain to me what is going on? Thanks in

Is it possible to add “keyed-subscripting” to Class objects?

走远了吗. 提交于 2019-12-01 06:51:44
In the vein of... @implementation MyClass - (id) objectForKeyedSubscript:(id)k { return [self something:k]; } Is it also possible to "subscript" Class objects? I too, am about to find out, with you.. but thought I would post this question as I tested it out, myself... + (id) objectForKeyedSubscript:(id)k { return [self.shared something:k]; } And alas.. it is not... id x = MyClass[@"document"]; error: unexpected interface name 'MyClass': expected expression But why, Daddy? Class ' sure get the short end of NSObject 's stick, if you ask me . Alex Gray Josh Caswell's comment pointed out the

Adding base class to existing object in python

只谈情不闲聊 提交于 2019-12-01 05:14:06
I have several objects of different kinds (different function names, different signatures) and I monkey patch them to have a common way to access them from different functions. Briefly, there is a dispatcher that takes the objects that I want to patch and depending on the object type it calls different patcher. A patcher will add methods to the object: def patcher_of_some_type(object): def target(self, value): # do something and call self methods object.target = types.MethodType(target, object) # many more of these As the program grows more complicated making a wrapper around the object (or