Underscores in Python numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gustav Hållberg

    #1

    Underscores in Python numbers

    I tried finding a discussion around adding the possibility to have
    optional underscores inside numbers in Python. This is a popular option
    available in several "competing" scripting langauges, that I would love
    to see in Python.

    Examples:
    1_234_567
    0xdead_beef
    3.141_592

    Would appreciate if someone could find a pointer to a previous
    discussion on this topic, or add it to a Python-feature-wishlist.

    - Gustav

  • Devan L

    #2
    Re: Underscores in Python numbers

    Gustav Hållberg wrote:[color=blue]
    > I tried finding a discussion around adding the possibility to have
    > optional underscores inside numbers in Python. This is a popular option
    > available in several "competing" scripting langauges, that I would love
    > to see in Python.
    >
    > Examples:
    > 1_234_567
    > 0xdead_beef
    > 3.141_592
    >
    > Would appreciate if someone could find a pointer to a previous
    > discussion on this topic, or add it to a Python-feature-wishlist.
    >
    > - Gustav[/color]

    I'm not sure what the _s are for, but I'm guessing they serve as
    separators ("." or "," depending on where you're from). I think the _s
    look ugly to me, besides, underscores look more like spaces than
    separators.

    Comment

    • Peter Hansen

      #3
      Re: Underscores in Python numbers

      Gustav Hållberg wrote:[color=blue]
      > I tried finding a discussion around adding the possibility to have
      > optional underscores inside numbers in Python. This is a popular option
      > available in several "competing" scripting langauges, that I would love
      > to see in Python.
      >
      > Examples:
      > 1_234_567
      > 0xdead_beef
      > 3.141_592
      >
      > Would appreciate if someone could find a pointer to a previous
      > discussion on this topic, or add it to a Python-feature-wishlist.[/color]

      Perhaps these threads, via Google?




      -Peter

      Comment

      • Dave Hansen

        #4
        Re: Underscores in Python numbers

        On 7 Nov 2005 18:02:09 -0800 in comp.lang.pytho n, "Gustav Hållberg"
        <gustav@gmail.c om> wrote:
        [color=blue]
        >I tried finding a discussion around adding the possibility to have
        >optional underscores inside numbers in Python. This is a popular option
        >available in several "competing" scripting langauges, that I would love
        >to see in Python.
        >
        >Examples:
        > 1_234_567
        > 0xdead_beef
        > 3.141_592
        >
        >Would appreciate if someone could find a pointer to a previous
        >discussion on this topic, or add it to a Python-feature-wishlist.[/color]

        I've never needed them in Python, but I've very often wished for them
        in C. Along with 0b(0|1)* for binary numbers, where they'd be even
        more useful.

        Of course, I write _far_ more code in C than Python. But I've seen
        enough bugs of the sort where someone wrote 1200000 when they meant
        12000000, that I see great value in being able to specify 12_000_000.

        Regards,
        -=Dave

        --
        Change is inevitable, progress is not.

        Comment

        • Roy Smith

          #5
          Re: Underscores in Python numbers

          Dave Hansen <iddw@hotmail.c om> wrote:[color=blue]
          > Of course, I write _far_ more code in C than Python. But I've seen
          > enough bugs of the sort where someone wrote 1200000 when they meant
          > 12000000, that I see great value in being able to specify 12_000_000.[/color]

          I'll admit that being able to write 12_000_000 would be convenient.
          On the other hand, writing 12 * 1000 * 1000 is almost as clear. In C,
          the multiplication would be done at compile time, so it's not even any
          less efficient. I'm not sure how Python handles that, but if it
          turned out to be a serious run-time performance issue, it's easy
          enough to factor it out into something that's done once and stored.

          Bottom line, embedded no-op underscores in numbers would be nice (and,
          IHMO, should be added), but the lack of such a feature should not be
          used as an excuse to write such unreadable monstrosities as 12000000
          in source code.

          Semi-related: see Jakob Nielsen's complaint about having to enter
          credit card numbers as 16-digit strings with no breaks on web forms
          (http://www.useit.com/alertbox/designmistakes.html, item #7, last
          bullet point).

          Comment

          • Dave Hansen

            #6
            Re: Underscores in Python numbers

            Sorry for the delayed response. I somehow missed this earlier.

            On Tue, 8 Nov 2005 15:39:09 +0000 (UTC) in comp.lang.pytho n,
            roy@panix.com (Roy Smith) wrote:
            [color=blue]
            >Dave Hansen <iddw@hotmail.c om> wrote:[color=green]
            >> Of course, I write _far_ more code in C than Python. But I've seen
            >> enough bugs of the sort where someone wrote 1200000 when they meant[/color][/color]

            Digression: 1 was enough.
            [color=blue][color=green]
            >> 12000000, that I see great value in being able to specify 12_000_000.[/color]
            >
            >I'll admit that being able to write 12_000_000 would be convenient.
            >On the other hand, writing 12 * 1000 * 1000 is almost as clear. In C,[/color]

            Perhaps, but it's pretty obvious that something's wrong when you have
            to resort to ugly tricks like this to make the value of a simple
            integer constant "clear."

            And think about 64 (or longer) -bit unsigned long long hexadecimal
            values. How much nicer is 0xFFF0_FF0F_F0F F_0FFF_ULL than
            0xFFF0FF0FF0FF0 FFFULL? I guess we could do something like
            ((((0xFFF0ULL<< 16)|0xFF0FULL)< <16)|0xF0FFULL) <<16)|0x0FFFULL ), but I'm
            not sure it's any better.

            Regards,
            -=Dave

            --
            Change is inevitable, progress is not.

            Comment

            • Dave Hansen

              #7
              Re: Underscores in Python numbers

              Sorry for the delayed response. I somehow missed this earlier.

              On Tue, 8 Nov 2005 15:39:09 +0000 (UTC) in comp.lang.pytho n,
              roy@panix.com (Roy Smith) wrote:
              [color=blue]
              >Dave Hansen <iddw@hotmail.c om> wrote:[color=green]
              >> Of course, I write _far_ more code in C than Python. But I've seen
              >> enough bugs of the sort where someone wrote 1200000 when they meant[/color][/color]

              Digression: 1 was enough.
              [color=blue][color=green]
              >> 12000000, that I see great value in being able to specify 12_000_000.[/color]
              >
              >I'll admit that being able to write 12_000_000 would be convenient.
              >On the other hand, writing 12 * 1000 * 1000 is almost as clear. In C,[/color]

              Perhaps, but it's pretty obvious that something's wrong when you have
              to resort to ugly tricks like this to make the value of a simple
              integer constant "clear."

              And think about 64 (or longer) -bit unsigned long long hexadecimal
              values. How much nicer is 0xFFF0_FF0F_F0F F_0FFF_ULL than
              0xFFF0FF0FF0FF0 FFFULL? I guess we could do something like
              ((((0xFFF0ULL<< 16)|0xFF0FULL)< <16)|0xF0FFULL) <<16)|0x0FFFULL ), but I'm
              not sure it's any better.

              Regards,
              -=Dave

              --
              Change is inevitable, progress is not.

              Comment

              • bonono@gmail.com

                #8
                Re: Underscores in Python numbers

                Personally, I would rather see the int() and float() function be
                smarter to take what is used for this, i.e. :

                a = int("1,234,567" )

                Of course, also support the locale variant where the meaning of "," and
                "." is swapped in most European countries.

                Gustav Hållberg wrote:[color=blue]
                > I tried finding a discussion around adding the possibility to have
                > optional underscores inside numbers in Python. This is a popular option
                > available in several "competing" scripting langauges, that I would love
                > to see in Python.
                >
                > Examples:
                > 1_234_567
                > 0xdead_beef
                > 3.141_592
                >
                > Would appreciate if someone could find a pointer to a previous
                > discussion on this topic, or add it to a Python-feature-wishlist.
                >
                > - Gustav[/color]

                Comment

                • bonono@gmail.com

                  #9
                  Re: Underscores in Python numbers

                  Personally, I would rather see the int() and float() function be
                  smarter to take what is used for this, i.e. :

                  a = int("1,234,567" )

                  Of course, also support the locale variant where the meaning of "," and
                  "." is swapped in most European countries.

                  Gustav Hållberg wrote:[color=blue]
                  > I tried finding a discussion around adding the possibility to have
                  > optional underscores inside numbers in Python. This is a popular option
                  > available in several "competing" scripting langauges, that I would love
                  > to see in Python.
                  >
                  > Examples:
                  > 1_234_567
                  > 0xdead_beef
                  > 3.141_592
                  >
                  > Would appreciate if someone could find a pointer to a previous
                  > discussion on this topic, or add it to a Python-feature-wishlist.
                  >
                  > - Gustav[/color]

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: Underscores in Python numbers

                    On Fri, 18 Nov 2005 16:26:08 -0800, bonono@gmail.co m wrote:
                    [color=blue]
                    > Personally, I would rather see the int() and float() function be
                    > smarter to take what is used for this, i.e. :
                    >
                    > a = int("1,234,567" )[/color]

                    But the problem isn't just with conversion of strings. It is also
                    with literals.

                    n = 99999999999

                    Without counting, how many nines?

                    Obviously repeated digits is an extreme case, but even different digits
                    are easier to process if grouped. That's why we write phone numbers like
                    62 3 9621 2377 instead of 62396212377.

                    Here is a thought: Python already concatenates string literals:

                    "abc" "def" is the same as "abcdef".

                    Perhaps Python should concatenate numeric literals at compile time:

                    123 456 is the same as 123456.

                    Off the top of my head, I don't think this should break any older code,
                    because 123 456 is not currently legal in Python.


                    --
                    Steven.

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: Underscores in Python numbers

                      On Fri, 18 Nov 2005 16:26:08 -0800, bonono@gmail.co m wrote:
                      [color=blue]
                      > Personally, I would rather see the int() and float() function be
                      > smarter to take what is used for this, i.e. :
                      >
                      > a = int("1,234,567" )[/color]

                      But the problem isn't just with conversion of strings. It is also
                      with literals.

                      n = 99999999999

                      Without counting, how many nines?

                      Obviously repeated digits is an extreme case, but even different digits
                      are easier to process if grouped. That's why we write phone numbers like
                      62 3 9621 2377 instead of 62396212377.

                      Here is a thought: Python already concatenates string literals:

                      "abc" "def" is the same as "abcdef".

                      Perhaps Python should concatenate numeric literals at compile time:

                      123 456 is the same as 123456.

                      Off the top of my head, I don't think this should break any older code,
                      because 123 456 is not currently legal in Python.


                      --
                      Steven.

                      Comment

                      • bonono@gmail.com

                        #12
                        Re: Underscores in Python numbers


                        Steven D'Aprano wrote:[color=blue]
                        > On Fri, 18 Nov 2005 16:26:08 -0800, bonono@gmail.co m wrote:
                        >[color=green]
                        > > Personally, I would rather see the int() and float() function be
                        > > smarter to take what is used for this, i.e. :
                        > >
                        > > a = int("1,234,567" )[/color]
                        >
                        > But the problem isn't just with conversion of strings. It is also
                        > with literals.
                        >
                        > n = 99999999999
                        >
                        > Without counting, how many nines?[/color]
                        For readability, I don't see why it cannot be written as :

                        n = int("99,999,999 ,999")

                        we already needs to do this for decimal("9.9")

                        Comment

                        • bonono@gmail.com

                          #13
                          Re: Underscores in Python numbers


                          Steven D'Aprano wrote:[color=blue]
                          > On Fri, 18 Nov 2005 16:26:08 -0800, bonono@gmail.co m wrote:
                          >[color=green]
                          > > Personally, I would rather see the int() and float() function be
                          > > smarter to take what is used for this, i.e. :
                          > >
                          > > a = int("1,234,567" )[/color]
                          >
                          > But the problem isn't just with conversion of strings. It is also
                          > with literals.
                          >
                          > n = 99999999999
                          >
                          > Without counting, how many nines?[/color]
                          For readability, I don't see why it cannot be written as :

                          n = int("99,999,999 ,999")

                          we already needs to do this for decimal("9.9")

                          Comment

                          • Stefan Rank

                            #14
                            Re: Underscores in Python numbers

                            on 19.11.2005 06:56 Steven D'Aprano said the following:
                            [snip][color=blue]
                            >
                            > Perhaps Python should concatenate numeric literals at compile time:
                            >
                            > 123 456 is the same as 123456.
                            >
                            > Off the top of my head, I don't think this should break any older code,
                            > because 123 456 is not currently legal in Python.[/color]

                            +1

                            but only allow (a single ?) space(s), otherwise readability issues ensue.

                            The other idea of teaching int() about separator characters has
                            internationalis/zation issues:
                            In many European countries, one would naturally try::

                            int('500.000,23 ')

                            instead of::

                            int('500,000.23 ')

                            --
                            stefan

                            Comment

                            • Stefan Rank

                              #15
                              Re: Underscores in Python numbers

                              on 19.11.2005 06:56 Steven D'Aprano said the following:
                              [snip][color=blue]
                              >
                              > Perhaps Python should concatenate numeric literals at compile time:
                              >
                              > 123 456 is the same as 123456.
                              >
                              > Off the top of my head, I don't think this should break any older code,
                              > because 123 456 is not currently legal in Python.[/color]

                              +1

                              but only allow (a single ?) space(s), otherwise readability issues ensue.

                              The other idea of teaching int() about separator characters has
                              internationalis/zation issues:
                              In many European countries, one would naturally try::

                              int('500.000,23 ')

                              instead of::

                              int('500,000.23 ')

                              --
                              stefan

                              Comment

                              Working...