PEP 378: Format Specifier for Thousands Separator

Carlos Nepomuceno carlosnepomuceno at outlook.com
Tue May 21 16:22:24 EDT 2013


----------------------------------------
> Date: Tue, 21 May 2013 14:53:54 -0500
> From: bahamutzero8825 at gmail.com
> To: python-list at python.org
[...]
>>
> What myth? People should indeed be using .format(), but no one said % formatting was going away soon. Also, the suggested change to the docs
> wasn't made and the issue is closed. The current docs do not say that % formatting isn't going to be deprecated, but it does mention its
> caveats and suggests .format(). If you are trying to say that % formatting will never ever go away, then you are wrong. It is highly
> unlikely to go away in a 3.x release, but /may/ get phased out in Python 4.0.

I vote for keeping str.__mod__()!

Anyway, is it possible to overload str.__mod__() without deriving a class? I mean to have something like:

old_mod = str.__mod__
def new_mod(x):
    global old_mod
    try:
        old_mod(x)
    except ValueError, ex:
        #catch ValueError: unsupported format character ',' (0x2c) at index 1
        #process new '%,d' format here
        return '{:,}'.format(x)  #just to illustrate the behaviour. it would have it's own faster code

str.__mod__ = new_mod  #this raises TypeError: can't set attributes of built-in/extension type 'str'
sys.stdout.write('num=%,d\n' % 1234567)


> --
> CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1
> --
> http://mail.python.org/mailman/listinfo/python-list 		 	   		  


More information about the Python-list mailing list