4

I stumbled upon this python:

__builtin__.__dict__['N_'] = lambda x: x
class X:
    doc = N_('some doc for class X')

I know conceptually what this does, but what I don't know is why? More precisely, what is the difference between that code and this:

class X:
    doc = 'some doc for class X'
2
  • 2
    Good question. As it stands, I'd say the author was trying to be clever and failed hard. Alternatively, he way way too clever. Commented May 3, 2011 at 19:58
  • This? git.gnome.org/browse/jhbuild/tree/jhbuild/main.py Commented May 3, 2011 at 20:01

3 Answers 3

6

Looks to me like the N_ function needs to be defined (it's probably supposed to look up translations), so he's creating it at the start of the process for anything else that happens in that process.

I'd assume that another piece of code, perhaps the code for non-English localisation, can replace the N_ function with one that looks up the appropriate translated string.

Sign up to request clarification or add additional context in comments.

3 Comments

See git.gnome.org/browse/jhbuild/commit/jhbuild/… where the function is introduced, the commit messages explicitly states it's for gettext (translation) support.
I almost added a "best guess" to my question which would have been some hook used later for translations.
I'm marking this answer as the best for now... but notice that commit uses _() throughout, not N_(). The real answer is in this commit: git.gnome.org/browse/jhbuild/commit/…
2

/agree with Thomas. It's the same as:

def N_(x): return x
__builtin__.__dict__['N_'] = N_

Why put it in __builtin__? Perhaps other modules need to use it as well.

Looking at the link KennyTM provides, there are some lines like:

... import config ...

after that. The config could change the built-in N_ function.

Comments

1

I agree with Thomas K. Essentially, the author wants a fall back when someone doesn't define a translation for doc, if they did, N_ from builtin dict is overridden by the one which does the translation. and if the translator has skipped something for translation, there is N_ function in the builtin, which is fallback which is imported, since builtin is the last scope to be checked for a function or var, the N_ will be found there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.