metaclass

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

旧巷老猫 提交于 2019-12-20 07:04:50
问题 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__

need to understand the flow of __init__, __new__ and __call__

二次信任 提交于 2019-12-18 11:13:13
问题 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):

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

笑着哭i 提交于 2019-12-18 10:13:32
问题 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

Groovy metaClass fails when overriding method called in constructor?

让人想犯罪 __ 提交于 2019-12-18 07:40:32
问题 I just tried to write this simple code to test overriding methods using metaClass. The code is here: class Hello { public Hello() { Foo() } public void Foo() { println "old" } } It has a Foo() method which simply prints "old" and it was called by the constructor. Here's the test code: class HelloTest { @Test public void test() { boolean methodFooWasCalled = false Hello.metaClass.Foo = {-> println "new" methodFooWasCalled = true } Hello hello = new Hello() assertTrue methodFooWasCalled == true

Subclassing type vs object in Python3 [duplicate]

ぐ巨炮叔叔 提交于 2019-12-18 07:08:08
问题 This question already has answers here : Python Type System - Object vs Type (2 answers) What are Python's type “objects” exactly? (3 answers) how does Cpython implement its type Objects, i.e. type's type is always type? (1 answer) Closed last year . I've been reading about metaclasses and I got lost when it came to type and object classes. I understand that they are at the top of the hierarchy and they are implemented in C code. I also understand that type inherits from object and that

What does Python's builtin __build_class__ do?

巧了我就是萌 提交于 2019-12-18 03:51:41
问题 In Python 3.1, there is a new builtin function I don't know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal helper function used by the class statement. What does this function do? Why must it be in builtins if it's internal? What is the difference to the type(name, bases, dict) function? 回答1: Compiling the PEP 3115 metaclass Guido van Rossum said: The PEP proposes that the class statement accepts keyword arguments,

Use class method not instance method with the same name

孤街浪徒 提交于 2019-12-17 21:15:20
问题 I have the following snippet: class Meta(type): def __getattr__(self, name): pass class Klass(object): __metaclass__ = Meta def get(self, arg): pass Now, if I do: kls = Klass() kls.get('arg') everything works as expected (the instance method get is called). But if I do: Klass.get('arg') again the instance method is found and an exception is given, since it is treated as an unbound method. How can I make a call to Klass.get('arg') go through the __getattr__ defined in the metaclass? I need

Provide __classcell__ example for Python 3.6 metaclass

两盒软妹~` 提交于 2019-12-17 19:59:32
问题 Per the 3.6.0 docs: CPython implementation detail : In CPython 3.6 and later, the __class__ cell is passed to the metaclass as a __classcell__ entry in the class namespace. If present, this must be propagated up to the type.__new__ call in order for the class to be initialized correctly. Failing to do so will result in a DeprecationWarning in Python 3.6, and a RuntimeWarning in the future. Can someone provide an example of doing this correctly? An example where it's actually needed? 回答1: The

What are some (concrete) use-cases for metaclasses?

元气小坏坏 提交于 2019-12-17 17:18:41
问题 I have a friend who likes to use metaclasses, and regularly offers them as a solution. I am of the mind that you almost never need to use metaclasses. Why? because I figure if you are doing something like that to a class, you should probably be doing it to an object. And a small redesign/refactor is in order. Being able to use metaclasses has caused a lot of people in a lot of places to use classes as some kind of second rate object, which just seems disastrous to me. Is programming to be

Using the __call__ method of a metaclass instead of __new__?

百般思念 提交于 2019-12-17 08:17:30
问题 When discussing metaclasses, the docs state: You can of course also override other class methods (or add new methods); for example defining a custom __call__() method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. My questions is: suppose I want to have custom behavior when the class is called, for example caching instead of creating fresh objects. I can do this by overriding the __new__ method of the class. When would I want to