---------------------------------------- > From: kwpol...@gmail.com > Date: Tue, 21 May 2013 21:06:11 +0200 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > To: carlosnepomuc...@outlook.com > CC: python-list@python.org > > On Tue, May 21, 2013 at 8:49 PM, Carlos Nepomuceno > <carlosnepomuc...@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)
I did: >>> def fmt(s): ... return '%s' % s ... >>> import dis >>> dis.dis(fmt) 2 0 LOAD_CONST 1 ('%s') 3 LOAD_FAST 0 (s) 6 BINARY_MODULO 7 RETURN_VALUE >>> Then I've looked for 'BINARY_MODULO' in "Python/ceval.c" and found: case BINARY_MODULO: w = POP(); v = TOP(); if (PyString_CheckExact(v)) x = PyString_Format(v, w); else x = PyNumber_Remainder(v, w); Then I've looked for 'PyString_Format' and found it in "Objects/stringobject.c" Analysing the code of "stringobject.c" I've found formatint() and formatlong(). I'm not using str.format() because it's slower than '%' and because I love '%'. str.format() looks like Java shit to me! >> 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, I'm just learning how to improve things! ;) >>>>> 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. Indeed! I just cut and pasted my code to create an example for the message. The actual format operation isn't done at the sys.stdout.write(). > You can also do a print, like everyone sane would. Where did you > learn Python from? “Python Worst Practice for Dummies”? lol I'm learning from my big mistakes up to now and a ton of online tutorials, besides "Dive into Python" (2004)[1], "Python Programming" (2012)[2] and "Programming Python, 3rd Ed" (2006) [print] print isn't thread safe. That's why I've chosen sys.stdout.write() -- it's faster and thread safe by default. I don't need any fancy formating, just need to send the string to screen. [1] http://www.diveintopython.net/ [1] http://en.wikibooks.org/wiki/Python_Programming > -- > Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16 > stop html mail | always bottom-post > http://asciiribbon.org | http://caliburn.nl/topposting.html > -- http://mail.python.org/mailman/listinfo/python-list