Modifying/Adding a property to global scope (was: Re: method gets overwritten?!)

Alexander Skwar usenet-posting.zu.askwar at spamgourmet.com
Fri Mar 14 18:30:00 CET 2003


Martin v. Löwis wrote:

> 
> OTOH, I'd advise against using .install; modifying the builtins is evil.
> Instead, do
> 
> _ = t.gettext
> 
> in each module where _ is meant to do translations.

Hm, how would I do this?  I'd like to be able define _ from one method
and use it from another.  I've used the following:

############################################################
# Module gettextTest
import gettext

class gettextTest:
    def __init__(self):
        print "__init__\n"
        self.SetLang(0)
        print "lang set\n"
        self.Output()
        print "did output\n"

    def SetLang(self, code):
        langs = ['de', 'en']
        lang = langs[code]
        try:
            t = gettext.translation(domain = 'xhc', localedir = 'locale',
                                    languages = [lang])
            _ = t.gettext
            print "_ installed\n"
        except IOError:
            print "IOError:\n"
            _ = self.untranslatedText

    def Output(self):
        print _("Exit")

    def untranslatedText(self, text):
        return text

############################################################
#!/usr/bin/env python
#Boa:PyApp:main

modules ={'gettextTest': [0, '', 'gettextTest.py']}

import gettextTest

def main():
    gtT = gettextTest.gettextTest()

if __name__ == '__main__':
    main()


When I run this mini "application", I get the following output and error
message:
############################################################

__init__

_ installed

lang set

Traceback (most recent call last):
  File "simpleGettextTest.py", line 12, in ?
    main()
  File "simpleGettextTest.py", line 9, in main
    gtT = gettextTest.gettextTest()
  File "C:\Dokumente und Einstellungen\skwara\Eigene Dateien\Python
Sources\X_Host_Chooser
\gettextTest.py", line 8, in __init__
    self.Output()
  File "C:\Dokumente und Einstellungen\skwara\Eigene Dateien\Python
Sources\X_Host_Chooser
\gettextTest.py", line 28, in Output
    print _("Exit")
NameError: global name '_' is not defined
############################################################

So, first gettextTest.__init__() is run, hence the output "__init__".
Then the "try:" block of SetLang() is executed, because I get the output
"_ installed".  Now SetLang() finished, and __init__() printed "lang set".

__init__ then calls Output().  Output does:
	print _("Exit")

This breaks, because the global name '_' is not defined, because
SetLang() did not modify/add a global property called '_' but instead
worked on a local property.

But when I use gettext.install() in SetLang(), everything works.


Well, my question basically boils down to this:

How do I add/modify a property in the global scope from within a method
of a class?  The reason why I want to do this, is because I'd like to use
_ for gettext.gettext.

Thanks,



Alexander Skwar
-- 
/* When we have more time, we can teach the penguin to say
 * "By your command" or "Activating turbo boost, Michael".
 */
	2.2.16 /usr/src/linux/arch/sparc/prom/sun4prom.c





More information about the Python-list mailing list