-
25 votesanswersviews
如何将装饰器中的变量传递给装饰函数中的函数参数?
我正在学习Python . 希望有人指出我正确的方式 .这就是我想在下面做的事情: def decorate(function): def wrap_function(*args, **kwargs): str = 'Hello!' # This is what I want return function(*args, **kwargs) ret... -
0 votesanswersviews
了解特定的装饰器,为什么函数内部有函数?
def lowercasewrapper(func): def wrapper(*args, **kwargs): return [item.lower() for item in func(*args, **kwargs)] return wrapper 我理解装饰器做了什么,我已经在我的代码中实现了装饰器并且它可以工作,但我对一些事情我不太确定 . 为什么不... -
2 votesanswersviews
带参数的装饰器[重复]
这个问题在这里已有答案: Decorators with parameters? 8个答案 代码如下 #!/usr/bin/env python3 def my_dec(func): def wrap(w): t=func(w) return t*4 return wrap @my_dec def testing(n): retu... -
5 votesanswersviews
python包装函数在装饰器内部获取参数
我正在尝试编写python装饰器,我在理解内部包装器如何获取参数时遇到问题 . 我在这里: import time def timing_function(some_function): def wrapper(): t1 = time.time() some_function() t2 = time.time() retu... -
2 votesanswersviews
使用修饰函数作为函数的默认参数
考虑这个模块: #mymodule.py import logging def print_start_end(name): """ Decorator that creates a logger and logs the start and the end of the function call ""&quo... -
1 votesanswersviews
Python设置docstring并获取动态生成的classmethod的方法名称
我正在尝试获取/设置动态创建的类方法的名称和文档字符串,如下所示,但我很难确定如何做到这一点: import sys import inspect class test(object): pass @classmethod def genericFunc(cls, **kwargs): print "function:", (inspect.stack()[... -
4 votesanswersviews
Mixin包装Scala特征的每个方法
假设我有几种方法的特征 Foo . 我想创建一个新的特性,扩展 Foo 但每个方法调用"wraps",例如使用一些print语句(实际上这将是更复杂的东西/我有几个不同的用例) . trait Foo { def bar(x: Int) = 2 * x def baz(y: Int) = 3 * y } 我可以通过覆盖每个方法手动完成此操作 . 但这似乎不必要地冗长... -
6 votesanswersviews
在python中使用与instance和classmethod相同的函数
人们可以这样做: class master: @combomethod def foo(param): param.bar() # Param could be type as well as object class slaveClass( master ): @classmethod def bar(cls): print(... -
637 votesanswersviews
在Python中创建单例
这个问题不是讨论singleton design pattern是否可取,是反模式,还是任何宗教战争,而是讨论如何以最pythonic的方式在Python中最好地实现这种模式 . 在这个例子中,我定义'most pythonic'意味着它遵循'principle of least astonishment' . 我有多个类可以成为单例(我的用例是 Logger ,但这并不重要) . 当我可以简单地... -
0 votesanswersviews
设置正确的Unity容器配置以解析装饰器模式中的接口类
下面给出了我不同的类声明以及我如何设置统一容器配置以获得Interface to Concrete类实现 . 代码当前抛出stackoverflow异常或建议无法构造接口 . 请帮我修复类结构或容器配置 . CodesController Class - public class CodesController : ApiController { private readonly IUni... -
95 votesanswersviews
保留装饰功能的签名
假设我编写了一个装饰器来做一些非常通用的东西 . 例如,它可能会将所有参数转换为特定类型,执行日志记录,实现memoization等 . 这是一个例子: def args_as_ints(f): def g(*args, **kwargs): args = [int(x) for x in args] kwargs = dict((k, int(v)) fo... -
3 votesanswersviews
python通过模拟测试装饰的装饰
这个问题是对Python中装饰器的this brilliant answer的后续跟进: 我使用给定的“片段使任何装饰者一般接受任何参数” . 然后我有这个(这里简化)装饰器: @decorator_with_args def has_permission_from_kwarg(func, *args, **kwargs): """Decorator to c... -
1 votesanswersviews
装饰师args和kwargs在元组内返回
我有以下装饰和课程 . def auth(fn): def check_headers(self): print 'checking headers...' #self.headers work done here def inner(self, *args, **kwargs): check_headers(self) ... -
3 votesanswersviews
打字稿装饰器不使用箭头功能
我有一个打字稿装饰工厂,控制台记录执行函数所花费的总时间,实际的函数执行结果和传递给装饰器的参数 . 例如 export function performaceLog(...args: any[]) { return (target: Object, key: string, descriptor: TypedPropertyDescriptor<any>) => { v... -
3 votesanswersviews
如何为属性创建TypeScript @enumerable(false)装饰器
我想在TypeScript中创建一个装饰器,以便能够使类属性不可枚举 . 我在这里找到了一个@enumerable的例子:https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators但这似乎只适用于方法,而不是属性: https://www.typescriptlang.org/docs/handboo... -
0 votesanswersviews
ES6属性装饰器@Prop()装饰器无法正常工作
我有以下Vue typescript组件类: import { Component, Prop, Vue } from 'vue-property-decorator'; import ChildComp from './ChildComp'; console.log(Prop); @Component({ template: ` <div> ... -
2 votesanswersviews
TypeScript错误:“作为表达式调用时,无法解析类装饰器的签名”
我想在我的React项目中使用Typescript . 使用HOC装饰器时出错 这会返回错误: Test.tsx中的错误:6:1 TS1238:当作为表达式调用时,无法解析类装饰器的签名 . 类型'typeof WithState'不能分配给'typeof Test'类型 . 'typeof WithState'类型中缺少属性'propTypes' . 'typeof WithState'类... -
2458 votesanswersviews
如何制作一系列功能装饰器?
如何在Python中创建两个装饰器来执行以下操作? @makebold @makeitalic def say(): return "Hello" ...应该返回: "<b><i>Hello</i></b>" 我不是试图在一个真实的应用程序中以这种方式制作 HTML - 只是想了解装饰器和装饰器链... -
174 votesanswersviews
如何实现打字稿装饰器?
TypeScript 1.5现在有decorators . 有人可以提供一个简单的例子来演示实现装饰器的正确方法,并描述可能的有效装饰器签名中的参数是什么意思吗? declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void; declare ty... -
714 votesanswersviews
@property装饰器如何工作?
我想了解内置函数 property 的工作原理 . 令我困惑的是 property 也可以用作装饰器,但它只在用作内置函数时才需要参数,而不是用作装饰器时 . 这个例子来自documentation: class C(object): def __init__(self): self._x = None def getx(self): return... -
1 votesanswersviews
类方法的python装饰器
我有一个装饰器来注册一些类方法 . 如何正确获取 self 和 run 参数? class Task(object): _tasks = [] @staticmethod def register(name): def decorator(fn): @wraps(fn) def wrapper(self=No... -
1 votesanswersviews
使用TypeVar在MyPy中使用参数键入装饰器会产生预期的无人居住类型
MyPy与 Callable *args 和 **kwargs 有一些问题,特别是有关装饰器的问题,详见:https://github.com/python/mypy/issues/1927 具体来说,对于没有参数只包装函数(并且不更改其签名)的装饰器,您需要以下内容: from typing import Any, Callable, cast, TypeVar FuncT = TypeV... -
0 votesanswersviews
角度中的元数据
我正试图将我的头脑包裹在打字稿中的装饰器和元数据中,并且思考角度是一个很好的项目,可以寻找某种最佳实践 . 在旧版本(例如2.1)中,他们使用的是DecoratorFactories,在内部他们将metadta应用于 reflect-metadata API提案(https://github.com/angular/angular/blob/2.1.x/modules/%40angular/co... -
1 votesanswersviews
TypeScript中的通用类型参数推断优先级
我有以下class decorator factory接受初始化函数作为其参数 . 在这个 initializer 函数中,我想返回一个对应于所涉及的类类型(或派生类型)的实例: function JsonObject<T>(initializer: (json: any) => T) { return function (target: { new (...args: ... -
0 votesanswersviews
React-native&Xcode:项目构建但无法创建存档
我正在使用mobx和装饰器 . 该项目通过“npm run ios”在模拟器中运行良好,并且它也在Xcode中构建而没有错误 . 当我尝试创建存档时,它失败了: “/ emobx-stores/UserStore.js中的SyntaxError:方法有装饰器,将装饰器插件放在类之前 . ” 这是我的bable.rc: { "presets": [ "b... -
3 votesanswersviews
React-MobX错误:'decorators'插件需要'decoratorsBeforeExport'选项,其值必须是布尔值
我收到以下错误:如果您要从Babylon / Babel 6迁移或想要使用旧的装饰器提议,您应该使用'decorators-legacy'插件而不是'decorators' . package.json "@babel/plugin-proposal-decorators": { "version": "7.1.2", ... -
3 votesanswersviews
获取已修饰类方法的类名
考虑这种情况: #!/usr/bin/env python # -*- coding: utf-8 -*- import functools def wrapmethod(f): @functools.wraps(f) def wrap(*args, **kwargs): print '>> %s' % (f.func_name) #... -
3 votesanswersviews
python,装饰和封闭混淆
我有一些测试代码: def num(num): def deco(func): def wrap(*args, **kwargs): inputed_num = num return func(*args, **kwargs) return wrap return deco @num(5) def ... -
8 votesanswersviews
在Sphinx文档中保留包装/修饰Python函数的默认参数
如何将 *args 和 **kwargs 替换为装饰函数文档中的真实签名? 假设我有以下装饰器和装饰功能: import functools def mywrapper(func): @functools.wraps(func) def new_func(*args, **kwargs): print('Wrapping Ho!') return ... -
2 votesanswersviews
我应该如何捕获可以引发的装饰器函数中的异常,如果装饰器函数在库中我无法修改?
我在Google App Engine(GAE)上工作python statsd library . 不幸的是,GAE可以在使用套接字时不时提升 ApplicationError: 4 Unknown error. . 错误是 apiproxy_errors.ApplicationError . statsd客户端已经设置为捕获 socket.error ,但不是套接字可以在GAE上引发的 A...