gpt4 book ai didi

python - 在 python 中,后台到底发生了什么使得 "x = 1j"工作,但 "x = 1*j"抛出错误?

转载 作者:太空狗 更新时间:2023-10-30 01:50:58 25 4
gpt4 key购买 nike

具体来说,如果我想定义一个对象,比如说 z,那么

x = 1z

工作但是

x = 1*z 

failed 抛出错误,我该如何定义这样的对象?

我不认为它涉及重载乘法运算符。

最佳答案

1j,因为它是 literal for a Complex Number 才有效(您在问题标题中提到了 1j)。有点像 [] 是列表的文字。

以下是 Python 文档/规范的相关摘录:

Imaginary literals are described by the following lexical definitions:

imagnumber ::= (floatnumber | intpart) ("j" | "J")

An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., (3+4j).

换句话说,1j 是一个特例,您无法使 1z1j 那样工作。 1z 是一个 SyntaxError,仅此而已(就 Python 而言,就是这样)。

关于python - 在 python 中,后台到底发生了什么使得 "x = 1j"工作,但 "x = 1*j"抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29781498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com