
[英]What are some similar alternatives to using classmethod and property decorators together?
除了一起使用类方法和属性装饰器之外,还有哪些类似的替代方法? 在 python 3.11 及更高版本中,不再支持组合它们:https://docs.python.org/3.11/library/functions.html#classmethod 我有这样的代码: tire_type impo ...
[英]What are some similar alternatives to using classmethod and property decorators together?
除了一起使用类方法和属性装饰器之外,还有哪些类似的替代方法? 在 python 3.11 及更高版本中,不再支持组合它们:https://docs.python.org/3.11/library/functions.html#classmethod 我有这样的代码: tire_type impo ...
[英]Decorating a class that has been defined already
我正在尝试从图书馆向一般 class 添加功能。 我定义了一个 function,它将库中的通用 class 作为输入,定义了一个 class,它以库中的通用 class 作为父级,并返回这样的 class。我想将这个 function 视为装饰器。 如果我将之前定义的 library_class ...
[英]Implementing decorators in terms of closures with pyo3
作为学习练习,我正在尝试使用闭包在 pyo3 中实现参数化装饰器 function。 pyo3 文档包含一个(非参数化)装饰器的示例,它使用__call__方法实现为 class,我在此基础上构建并使用外部 class 和__call__方法创建了一个参数化装饰器,该方法返回内部 class 和调 ...
[英]How can I use the decorator pattern in Python to dynamically add methods to a class based on data in a dictionary?
考虑这样一种情况,您有一个基本的 class Animal ,并且您希望根据存储在字典中的数据向 class 添加新方法。 字典中的数据将方法名映射到函数,你想使用装饰器模式将这些方法动态添加到class。 例如,假设字典定义如下:methods = { "walk": lambda se ...
[英]Clear list inside decorator from global scope
*1 这里我想要一个装饰器里面的清晰列表 a *2 所以这里必须是一个新的列表 a inside outer 是否可以清除全局 scope 中的列表 a? 如果是,如何纠正。 ...
[英]How can I set the default container in a decorator class for tkinter.Frame?
我想使用 Python package tkinter在 GUI 中创建一个可收缩面板。 我的想法是为tkinter.Frame class 创建一个装饰器,添加一个嵌套框架和一个切换嵌套框架的“垂直按钮”。 草图:(编辑:灰色框应该说Parent of contractible panel ) ...
[英]Using own decorators with pytest
我想在多个 pytest 测试文件中使用“助手”装饰器: 哪个是min311的最佳位置? 它不会从conftest.py自动导入。 ...
[英]How can I dynamically extend arguments of a method?
这些方法应该可以通过分配特定参数以及考虑类的属性来调用。 所以我想实现的是用预设属性覆盖方法的 arguments。 例如: 哪个打印 或者使用覆盖方法: 现在给 定义的 class Obj 看起来很笨拙。 特别是如果 class 的几种方法应该使用所描述的逻辑。 我如何概括f_1的逻辑,它基本上 ...
[英]Decorator's return is different from the actual return
代码为什么我得到不同的结果?from time import time def speed_test(func): def wrapper(*args, **kwargs): start = time() func(*args, **kwargs) ...
[英]Decorator pattern limitations
我有一个用例,我正在尝试使用装饰器模式但不确定它是否是使用模式的正确方法。 我有 2 个消息发布者 class。第一个发布者将 json 作为输入并发布消息第二个发布者将字节作为输入并发布消息目前这两个是单独的类 我想用 A 装饰 B,而不是创建一个单独的 class。基本上用户会输入 Json,它 ...
[英]Error with decorator in Typescript: Unable to resolve signature of property decorator when called as an expression.ts(1240)
尝试在 typescript 上创建一个简单的装饰器 function 时遇到问题。我不知道发生了什么。 这是装饰器: 当我像这样在另一个文件上调用它时 我收到以下错误: 当作为表达式调用时无法解析属性装饰器的签名.ts(1240) 如果我不强制装饰器的返回类型为任何类型,我会得到上一个错误以及这 ...
[英]Changing class attributes in decorator
我使用 django 作为一些 web 应用程序的框架。 我实现了自己的模型视图,因为我在同一个视图中有一些查询集和序列化器。 为此,我需要自己实现所有的 CRUD 功能: 请注意,我在对象刷新之前调用装饰器的 function。 我注意到在 function 调用之后设置的每个属性实际上都没有设置 ...
[英]How to decorate iterables with an error handler?
假设我们有两种方法:一种返回列表,另一种返回迭代器。 因此,在两个返回值都是可迭代的意义上,它们非常具有可比性。 我想编写一个装饰器来捕获迭代中的错误。 问题是迭代器在没有迭代的情况下返回,因此不会捕获任何错误。 在下面的代码中, wrapped_properly装饰器通过提供两个单独的包装器来解 ...
[英]Facing issue while running a code in python by using @classmethod decorator
我正在尝试在我的 jupyter 笔记本中运行以下命令,它工作正常,因为没有出现错误,但也没有 output。但是如果我在 IDE 中尝试相同的命令,它工作正常。 可能是什么问题? 我在 jupyter 中尝试过,我没有得到预期的 output,这是 csv 中提到的所有项目的字典形式的列表,所以我 ...
[英]Decorator Typescript
为什么打字稿的 function 装饰器不打印它装饰方法的 class 的这个?interface IUserService { users: number; get(): void; } class UserService implements IUserService { ...
[英]Decorator Pattern with added functions
我在教科书中找到了装饰器模式的这个例子。 如果我们考虑 object: LockedStack MyStack = new LockedStack(new UndoStack(new Stack())) 我如何调用 function 撤消它? MyStack.delegate.undo()将不起 ...
[英]NestJS - 'class-validator' shows an error in static class?
有没有办法用 static class 检查验证? Decorators are not valid here. ts(1206) Decorators are not valid here. ts(1206)错误仅发生在 static class。如何解决? 你们如何在 NestJS 中创建 ...
[英]Function to get the time complexity graph of any function/program
我是开发社区的新手,我想知道是否有任何 function 或方法可以用来决定哪种算法具有最佳性能,因此可以使用它而不是其他任何算法。 例如:我正在使用装饰器来了解函数解决问题需要多长时间,但我不认为这是可以推断的,因此,我在想也许有一种通用方法或 function 可以用来决定使用哪种算法。 你能帮 ...
[英]pass the value from parent component to child with input decorator with router
我试图将值从父组件的输入标签传递给子组件。 我正在使用父组件 ts 这给出了价值父 html 然后在子选择器中使用 [parentData]='parent' 将它传递给子组件,在子选择器中,我使用输入装饰器并且变量是子组件 孩子 HTML 我想通过路由将数据传递给子组件(我的意思是当我单击父 cl ...
[英]decorator argument: unable to access instance attribute
语境 MyMockClient下面的MyMockClient有两个方法: push_log :从列表self.logs中推送一个log并将其弹出 push_event :从列表self.events中推送一个event并将其弹出这两种方法都使用__iterate_pushes进行修饰,它基本上迭代 ...