Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45681

Re: PEP 378: Format Specifier for Thousands Separator

References <BLU176-W10190CB892A0414C988A05D7A80@phx.gbl> <nad-F52CE7.22440720052013@news.gmane.org> <CADUW+M=Y7b=4ZEENVzx4p6YpVD8T0eG=qz7sz8+iEow-5-o+jQ@mail.gmail.com> <BLU176-W9FF52BDCE42DAF40E6AC7D7A80@phx.gbl>
From Chris “Kwpolska” Warrick <kwpolska@gmail.com>
Date 2013-05-21 21:06 +0200
Subject Re: PEP 378: Format Specifier for Thousands Separator
Newsgroups comp.lang.python
Message-ID <mailman.1931.1369163194.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, May 21, 2013 at 8:49 PM, Carlos Nepomuceno
<carlosnepomuceno@outlook.com> wrote:
> Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead.

There is no real reason to do this.  `str.format()` is the new shiny
thing you should be using all the time.  Also, '%' is BINARY_MODULO
(where did you even get that name from?) if and only if you have two
numbers, and it performs the modulo division (eg. 27 % 5 = 2)

> So, the question is: Where would I change the CPython 2.7.5 source code to enable '%' (BINARY_MODULO) to format using the thousands separator like str.format() does, such as:
>
>>>>sys.stderr.write('%,d\n' % 1234567)
> 1,234,567

This will make your code unportable and useless, depending on one
patch you made.  Please don’t do that.  Instead,

> >>> sys.stdout.write('Number = %s\n' % '{:,.0f}'.format(x))
> Number = 12,345
>
> 'x' is unsigned integer so it's like using a sledgehammer to crack a nut!

In Python?  Tough luck, every int is signed.  And it isn’t just a
sledgehammer, it’s something worse.  Just do that:

>>> sys.stdout.write('Number = {:,.0f}\n'.format(x))

Much more peaceful.

You can also do a print, like everyone sane would.  Where did you
learn Python from?  “Python Worst Practice for Dummies”?

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: PEP 378: Format Specifier for Thousands Separator Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-05-21 21:06 +0200

csiph-web