Skip to content

gh-127833: lexical analysis: Improve section on Numeric literals #134850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

encukou
Copy link
Member

@encukou encukou commented May 28, 2025

Each section is expanded, and now progresses from simple cases and examples to more involved ones, ending with the formal grammar.

This uses meta-syntax that I want to add to the introduction in #134443. I'm marking it as draft until that's merged.


📚 Documentation preview 📚: https://cpython-previews--134850.org.readthedocs.build/

Comment on lines +1090 to +1094
Instead, :ref:`complex numbers <typesnumeric>` can be written
as adding the complex number's real part and imaginary part.

.. productionlist:: python-grammar
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
For example, the imaginary number 3.1+4\ *i* can be written as adding the
real number 3.1 to the imaginary number 4\ *i*.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this is a wrong picture.

The CPython has no pure-imaginary numbers and above decomposition is invalid in general. Simple counterexample:

>>> -0.0+0j
0j
>>> complex(-0.0, 0)
(-0+0j)

Previous description was valid: "An imaginary literal yields a complex number with a real part of 0.0."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key point is this is lexical analysis. Python indeed has no imaginary literals, but the tokeniser does -- 4+16j is three tokens.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think L1093 should say 'the complex number ...', though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but 16j is not an imaginary number. It's a complex number with a real part +0.0. And complex number in the Python currently can't be "written as adding the complex number's real part and imaginary part."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16j is not an imaginary number; but 16i is :)

I agree that this could be clearer. Will update. (Not sure when, as I recently volunteered to focus on f-strings first.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16j is not an imaginary number; but 16i is :)

But you are talking not about mathematics, but Python's complex numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants