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. Advertising

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. Alex
    Replies:
    1
    Views:
    300
    Peter Hansen
    Sep 9, 2005
  2. John J Lee
    Replies:
    3
    Views:
    448
    bruno at modulix
    Dec 1, 2005
  3. Edward Loper
    Replies:
    0
    Views:
    439
    Edward Loper
    Aug 7, 2007
  4. John J Lee
    Replies:
    0
    Views:
    498
    John J Lee
    Aug 7, 2007
  5. Edward Loper

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

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

Share This Page