【问题标题】:Python/Brython TypeError: Cannot use cached_property instance without calling __set_name__ on itPython/Brython TypeError:不能使用 cached_property 实例而不调用 __set_name__
【发布时间】:2019-12-14 14:25:45
【问题描述】:

我是一名网络工程师,刚接触编程,所以如果我遗漏了任何细节,请告诉我。尽我所能在这里解释这个问题:) 解决这个问题对我来说非常重要,所以任何输入都将高度赞赏

问题陈述: 我写了一个python脚本,它被一个brython脚本使用(在像javascript这样的html中使用,在编译时转换为javascript)。基本上当我点击我网页上的一个按钮时,它会触发一个 python 脚本,我在我的 html 中提到过这样的:

*<body onload="brython()">
<script type="text/python" src="ip_tools.py"></script>*

python 脚本如下所示:

from browser import document as doc, bind
import ipaddress
def subcalc(ev):
    #Using the ipaddress module I get the ip_network, so the value ip will be like 192.168.1.0/24 (an object of ipaddress class)
    ip = ipaddress.ip_network(doc['ipadd'].value + '/' + doc['ipv4_mask'].value, strict=False)

    #This line gives the aforementioned error in Chrome Console
    *print(ip.hostmask)*

doc["sub_cal"].bind("click", subcalc)  #This line triggers the subcalc function when user clicks on a button with id-"sub_cal" on the webpage.

来自 Chrome 控制台的完整错误:

error in get.apply Error
    at _b_.TypeError.$factory (eval at $make_exc (brython.js:7647), <anonymous>:161:327)
    at __get__491 (eval at exec_module (brython.js:8991), <anonymous>:5898:62)
    at __BRYTHON__.builtins.object.object.__getattribute__ (brython.js:5332)
    at Object.$B.$getattr (brython.js:6731)
    at subcalc0 (eval at $B.loop (brython.js:5230), <anonymous>:120:48)
    at HTMLButtonElement.<anonymous> (brython.js:12784) brython.js:5334 get attr hostmask of Object brython.js:5335 function () { [native code] } brython.js:6108 Traceback (most recent call last): TypeError: Cannot use cached_property instance without calling __set_name__ on it.

我有类似的脚本工作正常,唯一的区别是这个脚本使用导入的 Python 库,其他脚本不使用此类库,除了浏览器库(Brython 需要使用它html内容)

【问题讨论】:

    标签: python brython


    【解决方案1】:

    感谢您报告此问题,这是 Brython 中 PEP 487 实施中的一个错误。已在this commit 中修复。

    【讨论】:

    • 听起来不错!感谢您的快速响应和修复:) 但是我该如何使用这个修复?我看到这个修复是在 Brython 3.8.5 中给出的。 cdnjs.com/libraries/brython 没有 3.8.5。如果你是对的人,你能不能把 3.8.5 上传到 CDN 上。如果没有,您能否指导如何在我的系统上进行此更改,但是我最终会给我的用户带来缓慢的性能,因为他们必须从我的服务器下载它!再次感谢!
    • 抱歉,我无法为每个错误修复发布新版本。但是你可以在项目的 Github 页面给出的地址使用最新的开发版本。
    • 谢谢@marqueemoon,我现在明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2021-11-01
    • 1970-01-01
    • 2015-11-25
    • 2012-08-30
    相关资源
    最近更新 更多