In python, what exactly is happening in the background, such that "x = 1j" works, but "x = 1 * j" produces an error?

In particular, if I wanted to define an object, say z, such that

x = 1z

worked but

x = 1*z 

failed threw an error, how would I define such an object?

I do not think this is due to overloading the multiplier.

+4
source share
1 answer

1jworks because it is a literal for the complex number (you mentioned 1jin the title of the question). The view seems to []be a literal for the list.

Here's the relevant excerpt from the Python documentation / specification:

Imaginary literals are described by the following lexical definitions:

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

0.0. . , , (3+4j).

, 1j - , , 1z 1j. 1z SyntaxError ( Python, ).

+12

Source: https://habr.com/ru/post/1583851/


All Articles