2

I've found the following syntax in a python file:

 units = (
        (100, 1 << 30, _('%.0f GB')),
        (10, 1 << 30, _('%.1f GB')),
        (1, 1 << 30, _('%.2f GB')),
        (100, 1 << 20, _('%.0f MB')),
        (10, 1 << 20, _('%.1f MB')),
        (1, 1 << 20, _('%.2f MB')),
        (100, 1 << 10, _('%.0f KB')),
        (10, 1 << 10, _('%.1f KB')),
        (1, 1 << 10, _('%.2f KB')),
        (1, 1, _('%.0f bytes')),
        )

Does anyone know for what this underscore stands for?

Thanks in advance.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
rudimenter
  • 3,242
  • 4
  • 33
  • 46
  • 2
    underscore is a valid variable name in python. in this case it's most likely some i18n function – SilentGhost Jul 09 '10 at 13:07
  • 1
    I'm going to guess it's for i18n/localization. – Garrett Jul 09 '10 at 13:09
  • 1
    Most likely the same as these underscores: http://stackoverflow.com/questions/3077227/mercurial-python-what-does-the-underscore-function-do – Justin Ardini Jul 09 '10 at 13:10
  • Ironically enough, it is actual code from the [mercurial.util](http://www.google.com/codesearch/p?hl=en#yqvQ9RM69FY/mercurial/util.py&q=mercurial%20units%20_%20lang:Python&sa=N&cd=1&ct=rc&l=1863) – SilentGhost Jul 09 '10 at 13:14

5 Answers5

4

Underscore is a valid variable name, so you have to look at the context of your example code. Obviously the underscore is a method which has been defined somewhere else. Usually it's used for translation stuff or similar things.

Achim
  • 15,415
  • 15
  • 80
  • 144
  • thanks; in my humble opinion python & python developers overuse the underscore – rudimenter Jul 09 '10 at 13:44
  • It may get overused, but it's certainly convenient at times. In a User Interface I'm designing we do something like this to simplify translations: `from wx import GetTranslation as _`. – g.d.d.c Jul 09 '10 at 16:23
3

As said in other answers, _ is a valid name for a Python function. It's probable you will find _() used as translation function in some I18N packages.

apaderno
  • 28,547
  • 16
  • 75
  • 90
3

Look further up in the file. With some luck you'll find a statement like this:

from Language import _

Underscore is often used for i18n.

Ralph
  • 5,154
  • 1
  • 21
  • 19
2

As others have mentioned, the _ is a function. The usual convention is that it used for localisation and internationalisation

Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
0

The _ function is usually aliased to the GetText get function: http://docs.python.org/library/gettext.html

Htechno
  • 5,901
  • 4
  • 27
  • 37