我是一名网络工程师,刚接触编程,所以如果我遗漏了任何细节,请告诉我。尽我所能在这里解释问题 :) 找到这个问题的解决方案对我来说非常重要,因此任何输入都将非常感谢。
问题陈述: 我写了一个python脚本,它被brython脚本使用(在html中像javascript一样使用,在编译时转换为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 内容)
最佳答案
感谢报告,这是 Brython 中 PEP 487 实现中的一个错误。它固定在 this commit 中.
关于Python/Brython 类型错误 : Cannot use cached_property instance without calling __set_name__ on it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59335999/