Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented Jun 1, 2018

This PR adds support for underscores in numeric literals, like 123_000 and 0b1011_0011_1010. This is added in Python 3.6 by PEP 515 and is quite useful (especially in the domain of microcontrollers) so I think it's worth adding, even if we don't yet fully support Python 3.5 syntax.

To keep the code simple the patch allows through more possibilities than CPython, eg 123__456 is allowed, and so is 123_ (they give errors in CPython). I don't see this as a problem.

I also added support for formatting using _ ; eg '{:10_}'.format(12345678) gives '12_345_678'. I don't know if this is really important to add, but it was short and simple to do just to see if it's worth adding.

See #2415 for prior discussion on Python 3.6 support.

@stinos
Copy link
Contributor

stinos commented Jun 1, 2018

123__456 is allowed

Seems actually better than CPython to me: purpose is visual distinction so for long binary numbers this gives extra possibilities to distinguish the individual bytes etc, think 0b1111_1111__0000_0000

@dpgeorge
Copy link
Member Author

dpgeorge commented Jun 2, 2018

purpose is visual distinction so for long binary numbers this gives extra possibilities to distinguish the individual bytes

Nice idea!

@dpgeorge
Copy link
Member Author

I merged support for underscore in literals in 6a445b6

For the string formatting using underscore: I didn't merge this because 1) it adds code size; 2) I don't know how useful it would be, how often it would be used; 3) the implementation was not correct because it put underscores every 3 digits for all bases, whereas CPython puts them every 4 digits for hex, oct and bin formats (supporting this behaviour would add even more code).

@dpgeorge dpgeorge closed this Jun 12, 2018
@dpgeorge dpgeorge deleted the py-lexer-numeric-underscores branch June 12, 2018 02:24
@dpgeorge dpgeorge mentioned this pull request Oct 28, 2021
53 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants