How to use _ in interactive mode

Discussion in 'Python' started by pythonic, Nov 25, 2005.

  1. pythonic

    pythonic Guest

    Hi,

    I use '_' for localization in my program. The problem is when testing
    the program using python intractive mode I lose _ function.
    One solution is put following in PYTHONSTARTUP file.
    --
    import readline
    def __enforce_underscore__ (): __builtins__._ = str
    readline.set_pre_input_hook (__enforce_underscore__)
    --

    My program in startup imports a module which customize the env.
    Putting above in that module doesn't solve the problem. Any
    cleaner/better way?
    Honestly, I was expecting once overriden _ in builtins it would be honored.

    This how I reproduce the problem.

    Python 2.3.3 (#1, May 7 2004, 10:31:40)
    >>> def localizer(s): return str(s)
    >>> __builtins__.__dict__['_'] = localizer
    >>> _

    <function localizer at 0xb7f6e844>
    >>> _('some text')

    'some text'
    >>> _

    'some text'
    >>> _('some text')

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'str' object is not callable

    TIA.
     
    pythonic, Nov 25, 2005
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DGG
    Replies:
    3
    Views:
    982
  2. WJ
    Replies:
    2
    Views:
    764
  3. Christoph Becker-Freyseng

    dumping command-history in python interactive mode

    Christoph Becker-Freyseng, Jul 17, 2003, in forum: Python
    Replies:
    3
    Views:
    481
    Pedro Werneck
    Jul 18, 2003
  4. Matthew Barnes

    Detecting interactive mode reliably

    Matthew Barnes, Sep 8, 2003, in forum: Python
    Replies:
    0
    Views:
    342
    Matthew Barnes
    Sep 8, 2003
  5. Steve Menard
    Replies:
    0
    Views:
    336
    Steve Menard
    Jun 21, 2004
  6. Alex
    Replies:
    1
    Views:
    439
    Peter Hansen
    Sep 9, 2005
  7. John J Lee
    Replies:
    3
    Views:
    817
    bruno at modulix
    Dec 1, 2005
  8. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    653
    Edward Loper
    Aug 9, 2007
Loading...