对于Transcrypt Python到JavaScript编译器的 3.7.1版本,我目前正在使用新的@dataclass装饰器.根据PEP的摘要,我原本预计==, !=, <, >, >=, <=会得到支持,但似乎并非如此:
from dataclasses import dataclass
@dataclass
class C:
x: int = 10
Run Code Online (Sandbox Code Playgroud)
有些比较不起作用:
>>> c1 = C(1)
>>> c2 = C(2)
>>> c1 == c2 # ok
False
>>> c1 < c2 # crash
TypeError: '<' not supported between instances of 'C' and 'C'
Run Code Online (Sandbox Code Playgroud)
为什么不支持比较运算符,除了==和!=?或者我忽略了什么?
当我执行以下代码时,我得到一个备用矩阵:
import numpy as np
from scipy.sparse import csr_matrix
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sp = csr_matrix((data, (row, col)), shape=(3, 3))
print(sp)
(0, 0) 1
(0, 2) 2
(1, 2) 3
(2, 0) 4
(2, 1) 5
(2, 2) 6
Run Code Online (Sandbox Code Playgroud)
我想在这个稀疏矩阵中添加另一列,因此输出为:
(0, 0) 1
(0, 2) 2
(0, 3) 7
(1, 2) 3
(1, 3) 7
(2, 0) 4
(2, 1) …Run Code Online (Sandbox Code Playgroud) 我有以下几行代码:
import math as mt
...
...
...
if mt.isnan (coord0):
print (111111, coord0, type (coord0), coord0 in (None, mt.nan))
print (222222, mt.nan, type (mt.nan), mt.nan in (None, mt.nan))
Run Code Online (Sandbox Code Playgroud)
它打印:
111111 nan <class 'float'> False
222222 nan <class 'float'> True
Run Code Online (Sandbox Code Playgroud)
我很困惑......有什么解释吗?
Python 3.6.0,Windows 10
我对Python解释器的质量有着坚定的信心......而且我知道,只要计算机出现错误,实际上我就错了......那么我错过了什么?
[编辑]
(对@COLDSPEED的反应)
确实ID不同:
print (111111, coord0, type (coord0), id (coord0), coord0 in (None, mt.nan))
print (222222, mt.nan, type (mt.nan), id (mt.nan), mt.nan in (None, mt.nan))
Run Code Online (Sandbox Code Playgroud)
打印:
111111 nan <class 'float'> 2149940586968 False
222222 nan <class …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3.6b3进行长期运行的项目,在Windows上进行开发.对于这个项目,我也需要NumPy.我已经尝试过Python36 -m pip install numpy,但似乎pip尚未进入测试阶段.安装NumPy for Python 3.6b3的最佳方法是什么?
[编辑:使用ensurepip后添加了安装日志]
D:\aaa\numpy-1.12.0b1>call C:\Python36\python.exe -m pip install numpy
Collecting numpy
Using cached numpy-1.11.2.tar.gz
Installing collected packages: numpy
Running setup.py install for numpy: started
Running setup.py install for numpy: finished with status 'error'
Complete output from command C:\Python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\info_000\\AppData\\Local\\Temp\\pip-build-ueljt0po\\numpy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\info_000\AppData\Local\Temp\pip-nmezr3c7-record\install-record.txt --single-version-externally-managed --compile:
Running from numpy source directory.
Note: if you need reliable uninstall behavior, then install
with pip instead of using `setup.py …Run Code Online (Sandbox Code Playgroud) 这个问题不同于例如
因为如果知道所描述问题的原因是“g++ 和 gcc 之间的差异”,则无需再寻找答案。换句话说,虽然答案相同,但问题不同。
我有以下程序test.cpp:
struct CircuitElement {
bool value;
const char *name;
CircuitElement *next;
CircuitElement (const char *name);
virtual void evaluate () = 0;
};
struct Button: CircuitElement {
Button (const char *name);
virtual void evaluate ();
};
CircuitElement::CircuitElement (const char *name): name (name), next (0) {
}
Button::Button (const char *name): CircuitElement (name) {
}
void Button::evaluate () {
// Some statements
}
Button button ("button");
int main () {
return 0;
} …Run Code Online (Sandbox Code Playgroud) 在Transcrypt Python to JavaScript编译器中实现协同程序时,我遇到了以下奇怪的问题.
Transcrypt使用CPython 3.6的本机解析器生成AST.对于异步全局函数defs,它会生成AsyncFunctionDef节点.但对于异步方法,它没有!然而,CPython本身似乎正确地编译异步方法.
所以下面的一段代码用CPython运行,但是Transcrypt无法运行它,因为CPython的AST模块生成的AST似乎缺少方法的AsyncFunctionDef节点(而不是全局函数).
所以下面的代码不会生成AsyncFunctionDef节点:
class C:
def __init__ (self):
self.aTime = 2
async def g (self, waw, asio):
print ('g0')
await waw (self.aTime, asio)
print ('g1')
Run Code Online (Sandbox Code Playgroud)
我错过了什么?异步方法是官方支持的,不是吗?在PEP 492中找不到任何具体内容.
该示例的完整代码是:
from org.transcrypt.stubs.browser import __pragma__, __envir__
# Note that CPython will ignore all pragma's
# Provide waitAWhile for Transcrypt
__pragma__ ('js', '{}', '''
function waitAWhile (aTime, asio) {
return new Promise (resolve => {
setTimeout (() => {
resolve (aTime);
}, 1000 * …Run Code Online (Sandbox Code Playgroud) 我最近开始用 Python 编码。在我的脚本开始时,我总是有以下导入:import numpy as np.
在我的一个脚本中,我使用了np.matlib.repmat函数。它曾经可以正常工作,但是最近它无法运行并显示以下错误:
AttributeError: module 'numpy' has no attribute 'matlib'
Run Code Online (Sandbox Code Playgroud)
我搜索了这个问题,如果一个人在他的工作目录中有一个名为 numpy.py 的脚本,或者如果安装的版本不同并且不包含被调用的模块,看起来就会出现这样的错误。
我没有命名任何文件 numpy.py。我还发现,在我打电话之后:
from numpy import matlib as mb
Run Code Online (Sandbox Code Playgroud)
我可以使用mb.repmat. 因此,我的 numpy 模块确实包含matlib模块。有人可以提示我,为什么我不能打电话np.matlib?
在某些情况下,当我加载一个现有的 pickle 文件,然后再次转储它时,大小几乎减半。
我想知道为什么,第一个怀疑是协议版本。我可以以某种方式找出文件被腌制的协议版本吗?
我一直在谷歌搜索一段时间,例如'typeof'和'performance',但我还没有找到一个满意的答案来解决以下问题.
我正在尝试使用本地运算符重载为Transcrypt Python到JavaScript编译器实现复数.由于我们正在处理动态类型语言,因此无法预测变量中的数据类型.
如果我转换x + y为JavaScript,打开运算符重载,它将转换为例如__add__ (x, y)为了做正确的事情,__add__函数必须检查两者x以及y它们是否是"普通"JavaScript数字,或者它们中的一个或两者都是属于"复杂"类型,因为这需要特殊操作.
最明显的方法是测试typeof x == 'number'.然而,来自C/C++背景,用一个带有六个字符的字符串来测试相等性似乎是非常低效的,首先必须从内存中检索它,只有可能添加两个整数,这对于许多处理器来说,一次解析后,只会有一条指令.
让我感到惊讶的是,像这样的检查建议在互联网的各个地方作为正常的事情.有谁知道是否x == 'number'或可能x === 'number'以某种方式巧妙地优化,以防止完整的字符串比较.
为了进一步澄清问题,这里是我的__add__操作符的当前代码,使用字符串比较.
def __add__ (self, other):
if __typeof__ (other) == 'number': # Translates to: if (typeof other == 'number') {
return complex (self.real + other, self.imag)
else: # Other is complex
return complex (self.real + other.real, self.imag + other.imag)
Run Code Online (Sandbox Code Playgroud)
如果没有,任何人都可以通过更快的方式暗示我区分数字和任意非数字对象.
谢谢你的提示.来源现在是:
def __sub__ (self, …Run Code Online (Sandbox Code Playgroud) python ×9
transcrypt ×4
numpy ×3
javascript ×2
python-3.x ×2
async-await ×1
brython ×1
c++ ×1
gcc ×1
module ×1
pickle ×1
pip ×1
python-3.7 ×1
rapydscript ×1
scipy ×1