Uh oh!
There was an error while loading. Please reload this page.
gh-135676: lexical analysis: Improve section on Numeric literals - #134850
Conversation
| 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*. |
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
The key point is this is lexical analysis. Python indeed has no imaginary literals, but the tokeniser does -- 4+16j is three tokens.
There was a problem hiding this comment.
I think L1093 should say 'the complex number ...', though.
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
16j is not an imaginary number; but 16i is :)
But you are talking not about mathematics, but Python's complex numbers.
There was a problem hiding this comment.
I've reworded the intro for Imaginary literals. Is this clearer?
skirpichev
left a comment
There was a problem hiding this comment.
This pr mix
- change productlist directive to grammar-snippet, that solves referenced issue
- substantial rewrite of sections about literals.
I don't see which issue solves 2). But, maybe it's better to do in a separate pr?
encukou
commented
Jun 12, 2025
This PR is about the rewrite; changing the directives is a minor part of that. Perhaps I could use more granular issues -- have one for adding the |
encukou
commented
Jun 18, 2025
I've opened #135676 for rewording Lexical analysis, and attached this PR to that. |
encukou
commented
Jun 18, 2025
Merging; please note any follow-ups on the issue :) |
Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14. |
pythonGH-134850) (cherry picked from commit 21f3d15) Co-authored-by: Petr Viktorin <encukou@gmail.com>
GH-135677 is a backport of this pull request to the 3.14 branch. |
Uh oh!
There was an error while loading. Please reload this page.
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/