Using python lambda to define __builtin__ - but why?

I came across this python:

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

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

class X:
    doc = 'some doc for class X'
+3
source share
3 answers

It seems to me that the function N_should be defined (probably it should look for translations), so it creates it at the beginning of the process for something else that happens in this process.

I would suggest that another piece of code, perhaps code for non-English localization, can replace a function N_with one that looks at the corresponding translated string.

+5
source

I agree with Thomas. This is the same as:

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

__builtin__? , .

KennyTM, , :

... import config ...

. N_.

+1

I agree with Thomas K. Essentially, the author wants to return when someone does not define the translation of the document, if they do, N_ from the built-in dict is overridden by the one that performs the translation. and if the translator missed something for translation, in the built-in function there is N_, which is the backup, which is imported, since builtin is the last area to be checked for the function or var, N_ will be found there.

+1
source

All Articles