- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个method dispatch decorator具有三个注册功能。其中一个在 int
上调度,效果很好。第二个在自定义类型上调度,也可以正常工作。第三个也是自定义类型,但该类是用 lru_cache
装饰器包装的。
(为了让事情变得更复杂一些,该类是通过另一个类的 __call__ 方法上的 methoddispatch 以迂回方式实例化的。)
@lru_cache(maxsize=None, typed=True)
class QualifiedInterval:
# stuff that works
在 Pitch 类中:
@oph_utils.method_dispatch
def augmented(self, other):
raise NotImplementedError
@augmented.register(int)
def _(self, other):
return "works fine"
@augmented.register(Interval)
def _(self, other):
return "works fine too"
@augmented.register(QualifiedInterval)
def _(self, other):
return "only works if QualifiedInterval class does NOT have lru_cache"
(还有更多的事情发生,但这是不起作用的部分。)
基本上 - 如果我有 lru_cache,并将 QualifiedInterval 传递到函数中,它不会调度并引发 NotImplementedError。如果我注释掉缓存装饰器,它就可以工作。无论哪种方式,REPL 上的手动类型检查都会显示相同的类型(“QualifiedInterval”)。我尝试以几种不同的方式调用创建 QualifiedInterval 的命令,并尝试将其分配给变量。还是不行。我尝试在增强函数中进行显式类型检查。如果启用了缓存,类型检查也会失败。
最佳答案
Basically - if I have lru_cache, and pass a QualifiedInterval into the function, it does not dispatch
lru_cache是返回 decorator 的函数包装任何可调用的(包括类)。因此,当您将 lru_cache 应用于 QualifiedInterval 类时,该变量将分配给包装函数而不是类本身。
>>> @lru_cache(maxsize=None, typed=True)
class QualifiedInterval:
pass
>>> type(QualifiedInterval)
<class 'functools._lru_cache_wrapper'>
Single dispatch通过将第一个参数的类型与适当的方法相匹配来工作。但是,当您传入 QualifiedInterval 实例时,其类型与 functools._lru_cache_wrapper 不匹配,因此单个调度会回退到基本方法(这会引发 未实现。
教导单个调度匹配实际的原始类(类型)而不是包装的类:
@augmented.register(QualifiedInterval.__wrapped__)
def _(self, other):
return "show now work QualifiedInterval class has an lru_cache"
请注意添加了 .__wrapped__
属性,该属性通过包装函数到达原始未包装的类。
希望一切都清楚并指明前进的方向:-)
关于python - lru_cache 干扰 single_dispatch 完成的类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599852/
这是我的 + 部分: function insert(){ var linkElement = document.getElementById("BackButton"); var linkEl
我有两辆 buggy 在轨道上移动,它们都使用超声波测量模块来检测路径中的障碍物,并由 Arduino 微 Controller 控制。两个超声波传感器以相同的频率工作,并且这个频率不能改变。两个超声
希望能快速解决这个问题。我有一个用文件位置替换特定值的脚本。不幸的是,该位置似乎经常包含\n 或 n\(因为当前目录位于临时文件夹中),导致该行中断或从该行中完全删除自身,从而使文件夹位置无效。 临时
我使用的是 Mac OSX 10.10.1。我通常使用brew或pip来安装所有东西。我注意到我的 virtualenv 总是抓取我本地安装的所有 Python 包。我通过删除我的 $PYTHONPA
我正在尝试使用 str_replace 将两个字母的语言缩写转换为完整的单词。我遇到的问题是它们在回显时会相互影响。 $lang = str_replace("en", "English", $lan
我已经创建了用于结束通话的 aidl 文件 ITelephony.aidl。现在,在通话结束之前,我需要生成正在进行的通话中的语音,以便听众认为接下来的工作有问题。我尝试通过三种方式自己解决这个问题:
使用这个查询,我得到了我需要的每个客户下的订单数量的结果。但是,当我在 SELECT 字段中使用 SUM(OrderLine.ActualPrice) 并使用内部联接将 Order 表链接到包含价格的
在 Spring Boot 应用程序中,仅使用注释,我想实现安全性 我已将 @EnableGlobalMethodSecurity(jsr360Enabled=true) 添加到配置类。该类还有一个
我一直在 Linux 下开发一个多线程程序,对信号处理有特殊要求。例如,我需要程序打印单个 Ctrl-C 输入的统计信息。 我使用 pthread_sigmask 和 sigwait 实现了这一点,即
作为我第一次涉足 Dynamic Scala 领域,我想我会尝试通过 applyDynamic 访问 bean 属性。 我的第一个非常粗略的剪裁是 trait BeanProperties ext
我知道这是一个令人沮丧的话题,但我尝试遵循文档和其他类似的帖子。 我的问题是,除非重新加载页面,否则 JS 将无法工作。 我有以下 JS: $(function() { $('a.form-sho
我正在使用 JQuery Ajax 从服务器获取脚本。 $.ajax({ url: src, type: "GET", dataType: "script", time
我正在尝试根据每个产品的选定数量分别输出计算价格。我尝试复制代码并重命名所有变量,但输出是由各种增加/减少按钮触发的。 这是目前的代码: $(".incr-btn_mobile").on("click
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: jQuery in Greasemonkey 1.0 conflicts with websites usi
我在一个 iOS 应用程序 (Obj-C) 中有一个 View ,它在中心有一个 ImageView ,紧接着在其下方有一个 slider 。 ImageView 显示专辑封面, slider 可用于
我有一个 View Controller ,将 UITapGestureRecognizer 附加到它的主 UIView,并使用户能够点击屏幕以使状态、导航和工具栏重新出现/消失(如照片应用程序)。我
我正在尝试在我的应用程序中设置 log4j,但我的一个第三方 JAR 似乎以某种方式不断将我的日志级别设置为“关闭”。它仅在执行该库中的代码时发生。我不知道它是如何做到这一点的,因为我没有使用它知道名
我有一个 UIScrollView,其框架与包含的 View Controller 相同。 当 ScrollView 的缩放比例大于 1 时,从屏幕左侧 50% 开始从左向右滑动会导致 interac
我是网页设计的新手,最近接触了 Bootstrap。我最近也开始使用 Codepen。在 Codepen 中完成一个元素后,我将代码从那里复制并粘贴到 Sublime 中,无论出于何种原因,它更改了我
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
我是一名优秀的程序员,十分优秀!