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.

4
  • 2
    underscore is a valid variable name in python. in this case it's most likely some i18n function Commented Jul 9, 2010 at 13:07
  • 1
    I'm going to guess it's for i18n/localization. Commented Jul 9, 2010 at 13:09
  • 1
    Most likely the same as these underscores: stackoverflow.com/questions/3077227/… Commented Jul 9, 2010 at 13:10
  • Ironically enough, it is actual code from the mercurial.util Commented Jul 9, 2010 at 13:14

5 Answers 5

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.

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

2 Comments

thanks; in my humble opinion python & python developers overuse the underscore
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 _.
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.

Comments

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.

Comments

2

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

Comments

0

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

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.