New Python 3.0 string formatting - really necessary?

ajaksu ajaksu at gmail.com
Mon Dec 29 23:18:25 CET 2008


On Dec 29, 7:37 pm, Luis M. González <luis... at gmail.com> wrote:
> I still can't get used to add the parenthesis to "print", and this is
> the only thing I don't like, but I'm sure there's a good reason for
> this change...

I should know better than to post such an awful hack:

__past__.py:

from sys import excepthook as sys_excepthook
from sys import modules
---
def printhook(exctype, value, traceback):
    skip = True
    if isinstance(value, SyntaxError):
        if 'print ' in value.text:
            printable = value.text.replace('print ', '')[:-1]
            skip = False
            toprint = 'print(' + printable +')'
            print('Trying to convert your mess into', toprint)
            try:
                exec(toprint)
            except NameError as ne:
                name = str(ne).replace("name '", '').replace("' is not
defined", '')
                try:
                    var = str(getattr(modules['__main__'], name))
                    exec('print(' + printable.replace(name, var) +
')')
                except AttributeError as ae:
                    sys_excepthook(NameError, ne, traceback)
                except SyntaxError as se:
                    print('NameError workaround replaced something
bad')
                    skip = True
                except NameError as ne2:
                    print('Too many names to map to objects :P')
                    skip = True
                except:
                    print('Sorry, something went wrong and I am too
lazy to find out what')
                    skip = True
            except:
                raise
                skip = True
    if skip:
        sys_excepthook(exctype, value, traceback)
---

Then, as I'd check some stuff in parallel on 2.5 and 3.0, I do this on
the 3.0 prompt:
---
import sys
exchook = sys.excepthook
from __past__ import printhook
sys.excepthook = printhook
---

As soon as I wrote that mess^H^H^H^H helper, remembering to use print
() became easier (I think the trauma helped) and I haven't imported
much from __past__ since.

Should I hit 'send'?

Daniel



More information about the Python-list mailing list