decorator

Python: Regular method and static method with same name

走远了吗. 提交于 2020-01-01 07:39:06
问题 Introduction I have a Python class, which contains a number of methods. I want one of those methods to have a static counterpart—that is, a static method with the same name—which can handle more arguments. After some searching, I have found that I can use the @staticmethod decorator to create a static method. Problem For convenience, I have created a reduced test case which reproduces the issue: class myclass: @staticmethod def foo(): return 'static method' def foo(self): return 'public

Decorator functions in Go

眉间皱痕 提交于 2020-01-01 04:54:06
问题 Decorator pattern (functions) has many benefits: It is very useful when a method has many orthogonal concerns... I.e., None of these concerns are related, other than that we wanna do all (or some) of them whenever we call our method. This is where the decorator pattern really helps. By implementing the decorator pattern we subscribe to the open-closed principal. Our method is open to future extension but closed to future modification. There's a lot of groovy benefits to obeying the open

Inside a decorator-class, access instance of the class which contains the decorated method

浪尽此生 提交于 2020-01-01 02:45:10
问题 I have the following decorator, which saves a configuration file after a method decorated with @saveconfig is called: class saveconfig(object): def __init__(self, f): self.f = f def __call__(self, *args): self.f(object, *args) # Here i want to access "cfg" defined in pbtools print "Saving configuration" I'm using this decorator inside the following class. After the method createkvm is called, the configuration object self.cfg should be saved inside the decorator: class pbtools() def __init__

Adding State in Decorator Pattern

ε祈祈猫儿з 提交于 2019-12-31 02:54:16
问题 I wonder how to add state to the chain of decorators that will be available to the consumer. Given this simplified model: abstract class AbstractPizza { public abstract print(...); } class Pizza : AbstractPizza { public int Size { get; set; } public print(...); } abstract class AbstractPizzaDecorator { public Pizza:AbstractPizza; public abstract print(); } class HotPizzaDecorator : AbstractPizzaDecorator { public int Hotness { get; set; } public print(...); } class CheesyPizzaDecorator :

Decorator error: NoneType object is not callable

自古美人都是妖i 提交于 2019-12-30 11:55:11
问题 I wrote a function decorator like this: def tsfunc(func): def wrappedFunc(): print '%s() called' % func.__name__ return func() return wrappedFunc() @tsfunc def foo(): pass foo() # to get it work, use foo instead of foo() foo() I got following error message: foo() called Traceback (most recent call last): File "decorator.py", line 11, in <module> foo() TypeError: 'NoneType' object is not callable I get it work by replacing "foo()" with "foo". but I still didn't get the result I expected: foo()

toggling decorators

假如想象 提交于 2019-12-30 08:22:35
问题 What's the best way to toggle decorators on and off, without actually going to each decoration and commenting it out? Say you have a benchmarking decorator: # deco.py def benchmark(func): def decorator(): # fancy benchmarking return decorator and in your module something like: # mymodule.py from deco import benchmark class foo(object): @benchmark def f(): # code @benchmark def g(): # more code That's fine, but sometimes you don't care about the benchmarks and don't want the overhead. I have

toggling decorators

空扰寡人 提交于 2019-12-30 08:21:09
问题 What's the best way to toggle decorators on and off, without actually going to each decoration and commenting it out? Say you have a benchmarking decorator: # deco.py def benchmark(func): def decorator(): # fancy benchmarking return decorator and in your module something like: # mymodule.py from deco import benchmark class foo(object): @benchmark def f(): # code @benchmark def g(): # more code That's fine, but sometimes you don't care about the benchmarks and don't want the overhead. I have

Extension Methods - Decorator Pattern

柔情痞子 提交于 2019-12-30 08:13:50
问题 I was wondering if we can consider extension methods as an implementation of the decorator pattern in C#? as the aim is the same but logic of implementation as well as the conception may differ? Thanks! 回答1: The decorator pattern is usually associated with adding behavior to particular instances of a type independently of other instances. In the case of an extension method it affects all instances of a type which are compiled in the same scope. I'd argue that it's not a part of the decorator

__decorated__ for python decorators

时间秒杀一切 提交于 2019-12-30 05:00:48
问题 As of 2.4 (2.6 for classes), python allows you to decorate a function with another function: def d(func): return func @d def test(first): pass It's a convenient syntactic sugar. You can do all sorts of neat stuff with decorators without making a mess. However, if you want to find out the original function that got decorated you have to jump through hoops (like Cls.method.__func__.__closure__[0].cell_contents or worse). I found myself wishing for a better way and found that there had been some

Python: Decorating a class method that is intended to be overwritten when inherited

吃可爱长大的小学妹 提交于 2019-12-30 04:41:08
问题 Let's say I have some base class: class Task: def run(self): #override this! Now, I want others to subclass Task and override the run() method: class MyTask(Task): def run(self): #successful override! However, the problem is that there is logic that must take place before and after the run() method of every class that subclasses Task. It seems like one way I could do this would be to define another method in the base class which then calls the run() method. However, I wanted to ask, is there