metaclass

Understanding __call__ with metaclasses [duplicate]

别说谁变了你拦得住时间么 提交于 2020-06-24 09:54:14
问题 This question already has an answer here : need to understand the flow of __init__, __new__ and __call__ (1 answer) Closed 2 years ago . From my understanding the __call__ method inside a class implements the function call operator, for example: class Foo: def __init__(self): print("I'm inside the __init__ method") def __call__(self): print("I'm inside the __call__ method") x = Foo() #outputs "I'm inside the __init__ method" x() #outputs "I'm inside the __call__ method" However, I'm going

Question about multiple inheritance, dynamic class creating and instantiation

别来无恙 提交于 2020-03-23 16:55:51
问题 Hello I have the following situation: A specialized class that inherits from two parent class The need to define the most specialized class at run time, based on some information that I get only when I start reading data from a database. I defined the following code to handle the create all the classes in the chain: class BusinessDocument(): @staticmethod def get_class(doc_type): switch = { 'MasterData': MasterData, 'Transactional': Transactional } func = switch.get(doc_type, lambda: "Invalid

Registry pattern with __init_subclass__ and sub-classable registry

本秂侑毒 提交于 2020-02-05 03:17:06
问题 I want to create a settings registry. I also want to be able to group settings in macro-categories. The following simplified example works with a single registry: class RegistryHolder: registry = {} def __init_subclass__(cls, setting_name=None, **kwargs): super().__init_subclass__(**kwargs) cls.registry[setting_name] = cls class SettingOne(RegistryHolder, setting_name='SettingOne'): pass class SettingTwo(RegistryHolder, setting_name='SettingTwo'): pass And the result is: print(RegistryHolder

Python subclass method to inherit decorator from superclass method

你说的曾经没有我的故事 提交于 2020-02-03 19:52:39
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

Python subclass method to inherit decorator from superclass method

江枫思渺然 提交于 2020-02-03 19:50:03
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

Python subclass method to inherit decorator from superclass method

旧时模样 提交于 2020-02-03 19:49:57
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

How can I create my own “parameterized” type in Python (like `Optional[T]`)?

不羁的心 提交于 2020-02-03 08:00:26
问题 I want to create my own parameterized type in Python for use in type hinting: class MaybeWrapped: # magic goes here T = TypeVar('T') assert MaybeWrapped[T] == Union[T, Tuple[T]] Never mind the contrived example; how can I implement this? I looked at the source for Union and Optional, but it looks like some fairly low-level hackery that I'd like to avoid. The only suggestion in the documentation comes from an example re-implementation of Mapping[KT,VT] that inherits from Generic. But that

How to loop through subviews in order to get the text of NSTextViews

半城伤御伤魂 提交于 2020-01-16 11:56:58
问题 I have a NSView which contains several instances of NSTextView. I would like to get the content (string) of each instance. So far, I have this (this code does not compile) : for(NSView *view in [self subviews]) { NSLog(@"class: %@ ", [view className]); if([view isKindOfClass:[NSTextView class]]) NSLog(@"[view string] %@",[view string]);} At this point, I expect to be able to send the string message to view which is an instance of NSTextView, but: Error message: No visible @interface for

Grails - Making Methods Globally Available and Metaclass Programming

和自甴很熟 提交于 2020-01-15 08:15:42
问题 I inserted this line into my init() of my BootStrap class Integer.metaClass.minutes = { 60000L * delegate } I was then not able to use it from a Job class (Quartz plugin). Do I put this line of code somewhere else to make it a global modification? I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way? 回答1: Do I put this line of code somewhere else to make it a

Why Groovy's map does not have metaClass?

强颜欢笑 提交于 2020-01-13 09:09:15
问题 Why does Groovy's literal map does not have a metaClass? // lists work as expected: aList = [] println aList.class // class java.util.ArrayList println aList.metaClass // gives the full blown metaclass // org.codehaus.groovy.runtime.HandleMetaClass@3de6696c // [groovy.lang.MetaClassImpl@3de6696c[class java.util.ArrayList]] // string and numbers too: println ''.metaClass println 12.metaClass // map does not: aMap = [:] println myMap.metaClass // gives null println myMap.class // also gives