decorator

Why does Angular 2 use decorators?

别说谁变了你拦得住时间么 提交于 2020-03-13 06:01:13
问题 I just started using Angular 2 and was wondering why some properties like selector and template are put in components decorators and not in components classes. What's the point of using all these decorators in Angular 2? 回答1: To make it easy for tools to provide all kinds of support in templates like: error checking auto-completion graphical GUI designers To generate code from decorators which allows: to define some things more declaratively or generate different code depending on some

Is there a simple means of adding an aspect to an existing VB.NET method?

若如初见. 提交于 2020-02-25 06:50:49
问题 I know there are numerous aspect-oriented frameworks for VB.NET. It's a little heavy to pull an entire framework into play in order to add an aspect to a couple methods. Does VB.NET offer a simple means (via some sort of metaprogramming/reflection) in which to layer an aspect over an existing method in a class/object? Basically, the goal is intercept a method's incoming message to invoke it and add side effects or to manipulate the request, just as one would normally do in standard AOP. Are

Is there a simple means of adding an aspect to an existing VB.NET method?

安稳与你 提交于 2020-02-25 06:50:27
问题 I know there are numerous aspect-oriented frameworks for VB.NET. It's a little heavy to pull an entire framework into play in order to add an aspect to a couple methods. Does VB.NET offer a simple means (via some sort of metaprogramming/reflection) in which to layer an aspect over an existing method in a class/object? Basically, the goal is intercept a method's incoming message to invoke it and add side effects or to manipulate the request, just as one would normally do in standard AOP. Are

Is there a simple means of adding an aspect to an existing VB.NET method?

牧云@^-^@ 提交于 2020-02-25 06:50:16
问题 I know there are numerous aspect-oriented frameworks for VB.NET. It's a little heavy to pull an entire framework into play in order to add an aspect to a couple methods. Does VB.NET offer a simple means (via some sort of metaprogramming/reflection) in which to layer an aspect over an existing method in a class/object? Basically, the goal is intercept a method's incoming message to invoke it and add side effects or to manipulate the request, just as one would normally do in standard AOP. Are

JavaScript Decorator on Class constructor

我是研究僧i 提交于 2020-02-04 04:14:28
问题 I'm trying to add some properties in class instances (like a Plugin system). For that, I followed this example to do that with a Class Decorator: function testDecorator(target:any) { // save a reference to the original constructor var original = target; // the new constructor behaviour var f : any = function (...args: any[]) { console.log("New: " + original.name); return original.apply(this, args) } // copy prototype so intanceof operator still works f.prototype = original.prototype; //

Lazy class property decorator

蹲街弑〆低调 提交于 2020-01-30 06:38:45
问题 I have one django model which needs to do some processing referring the custom user model. I can't work with the class of this model at class loading time because the loading order of the classes is unknown. So I need to add some class attributes at runtime, at the moment I'm adding them in the __init__ or __new__ like: def __new__(cls, *args, **kwargs): # hack to avoid INSTALLED_APPS initialization conflicts. # get_user_model() can't be called from this module at class loading time, # so

Decorating a generator in Python: call some method in between yields

ぐ巨炮叔叔 提交于 2020-01-24 10:12:06
问题 I found some very useful information about decorating generator functions in Python here using yield from . For example: def mydec(func): def wrapper(*args, **kwargs): print(f'Getting values from "{func.__name__}"...') x = yield from func(*args, **kwargs) print(f'Got value {x}') return x return wrapper @mydec def mygen(n): for i in range(n): yield i However, this seems to only allow for adding decorated behaviors at the beginning and end of the generator's lifetime: >>> foo = mygen(3) >>> x =

Check if a function was called as a decorator

别说谁变了你拦得住时间么 提交于 2020-01-24 03:32:50
问题 In the following minimal example decorate is called two times. First using @decorate , second by normal function call decorate(bar) . def decorate(func): print(func.__name__) return func @decorate def bar(): pass decorate(bar) Is it possible to see inside of decorate if the call was invoked by using @decorate or as a normal function call? 回答1: The @decorator syntax is just syntactic sugar, thus both examples have identical behaviour. This also means whatever distinction you are doing between

How to get type data in TypeScript decorator?

ⅰ亾dé卋堺 提交于 2020-01-23 05:42:08
问题 I would like to be access the type information on a variable declaration that I want to decorate: @decorator foo: Foo; From the decorator, can I somehow access Foo ? 回答1: You should be able to do it, but you'll need to use reflect-metadata. There's an example here: Decorators & metadata reflection in TypeScript: From Novice to Expert which seems to be exactly what you're after: function logType(target : any, key : string) { var t = Reflect.getMetadata("design:type", target, key); console.log(

Python: Force decorator inheritance?

混江龙づ霸主 提交于 2020-01-22 20:09:10
问题 I'm working with quite a large OOP code-base, and I'd like to inject some tracing/logging. The easiest way to do this would be to introduce a decorator around certain methods on some base classes, but unfortunately decorators aren't inherited. I did try something like the following: def trace(fn): def wrapper(instance, *args, **kwargs): result = fn(instance, *args, **kwargs) # trace logic... return result return wrapper class BaseClass(object): def __init__(self, ...): ... self.__call__ =