metaclass

Body of abstract method in Python 3.5 [duplicate]

一曲冷凌霜 提交于 2020-01-01 02:52:28
问题 This question already has an answer here : What should I put in the body of an abstract method in Python (1 answer) Closed 2 years ago . I have an abstract class, Model , with a few abstract methods, what should I put in the body of the methods? A return class Model(metaclass=ABCMeta): @abstractmethod def foo(self): return A pass class Model(metaclass=ABCMeta): @abstractmethod def foo(self): pass Raising a descriptive error class Model(metaclass=ABCMeta): @abstractmethod def foo(self): raise

Base metaclass overriding __new__ generates classes with a wrong __module__

北战南征 提交于 2019-12-31 04:51:25
问题 I found a weird behavior with cpython 2.5, 2.7, 3.2 and pypy with metaclass that override __new__ when using the python 2 / python 3 compatible way of using metaclass : Given a module m1: class C1Meta(type): def __new__(cls, name, bases, dct): return type.__new__(cls, name, bases, dct) C1 = C1Meta('C1', (object,), {}) class C2Meta(type): pass C2 = C2Meta('C2', (object,), {}) And the following main program: import m1 C10 = m1.C1Meta('C10', (m1.C1,), {}) class C11Meta(m1.C1Meta): pass C11 =

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

爱⌒轻易说出口 提交于 2019-12-30 17:23:31
问题 This question already has answers here : How to create a custom string representation for a class object? (5 answers) Closed 3 years ago . 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

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

。_饼干妹妹 提交于 2019-12-30 10:07:12
问题 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

Setting a class __name__ declaratively

纵然是瞬间 提交于 2019-12-30 09:10:10
问题 Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier? >>> class Potato: ... __name__ = 'not Potato' ... >>> Potato.__name__ # doesn't stick 'Potato' >>> Potato().__name__ # .. but it's in the dict 'not Potato' I thought maybe it was simply a case that this was overwritten after the class definition block completes. But seems that's not true, because the name is writable yet apparently not set in the class dict: >>> Potato.__name__ = 'no

Java implementation - Meta classes

北慕城南 提交于 2019-12-30 01:01:05
问题 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..

Groovy: this.metaClass Versus instance.metaClass

我与影子孤独终老i 提交于 2019-12-29 07:36:09
问题 I have encountered below groovy script code in the book . And it generated some strange outputs to me. class Person{ def work(){ println "work()" } def sports=['basketball','football','voleyball'] def methodMissing(String name, args){ if(name in sports){ println "injected ${name} into Person class" Person instance=this println "this.metaClass:\t\t${this.metaClass}" println "instance.metaClass:\t${instance.metaClass}" assert this.metaClass==instance.metaClass }else{ println "no such method:$

Intercept operator lookup on metaclass

北城余情 提交于 2019-12-29 01:34:10
问题 I have a class that need to make some magic with every operator, like __add__ , __sub__ and so on. Instead of creating each function in the class, I have a metaclass which defines every operator in the operator module. import operator class MetaFuncBuilder(type): def __init__(self, *args, **kw): super().__init__(*args, **kw) attr = '__{0}{1}__' for op in (x for x in dir(operator) if not x.startswith('__')): oper = getattr(operator, op) # ... I have my magic replacement functions here # `func`

Are classes objects in Objective-C?

允我心安 提交于 2019-12-27 20:12:12
问题 okay, so I understand that an object is an instance of a class that must be allocated and initialized, but are classes themselves objects? I know when you create a new class it is an instance of something else, like NSObject. So, if this makes it a class, then objects can hold not only variables and methods, but other objects as well, right? Sorry, this is probably really basic, but I am reading two books about cocoa and xcode and this point is a little unclear (probably because of my lack of

calling help(MyClass) also shows base class attributes: how to avoid that?

微笑、不失礼 提交于 2019-12-25 02:09:44
问题 MyClass is derived from "list": MyClass(list) I would like to document MyClass nicely. Unfortunately, when trying help(MyClass), I get my own documentation, but I also get a lot of stuff about "list". Would there be a simple way to control that? I read something about metaclasses, but I was unable to do something. Thanks for your suggestions, Michel 回答1: Well, that is what help does. It introspects into your class and show the name and the associated __doc__ for each callable attribute in the