tags:

views:

116

answers:

2

Hi all,

In the Getting things gnome code base I stumbled upon this import statement

from GTG import _  

and have no idea what it means, never seen this in the documentation and a quick so / google search didn't turn anything up.

Thank you all in advance Paul

+4  A: 

This imports the function/class/module _ into the current namespace. So instead of having to type GTG._, you just have to type _ to use it.

Here is some documentation:

http://docs.python.org/tutorial/modules.html#more-on-modules

It should be noted that you should use this with care. Doing this too much could pollute the current namespace, making code harder to read, and possibly introducing runtime errors. Also, NEVER NEVER NEVER do this:

from MODULE import *

, as it very much pollutes the current namespace.

This technique is most useful when you know you are only going to use one or two functions/classes/modules from a module, since doing this only imports the listed assets.

For example, if I want to use the imap function from the itertools module, and I know I won't need any other itertools functions, I could write

from itertools import imap

and it would only import the imap function.

Like I said earlier, this should be used with care, since some people may think that

import itertools

# ... more code ...

new_list = itertools.imap(my_func, my_list)

is more readable than

from itertools import imap

# ... more code ...

new_list = imap(my_func, my_list)

as it makes it clear exactly which module the imap function came from.

W_P
Paul helping Paul, I'm onto you...
Daniel DiPaolo
lol i thought it fitting that i answer the question...
W_P
It's worth mentioning that `_` doesn't have a special meaning in Python, it's just another letter-like character that you can use to name a variable.
David Zaslavsky
@David good point, thanks for the clarification.
W_P
Hey Daniel, good call, but I (the one who asked the question) am not the one who wrote the answer, whoever that was, thanks a lot.
Paul
@Paul I actually updated my display name so I would have less of these problems from now on lol...
W_P
@Paul,thanks for pointing this out. I was aware of the general 'from GTG import bla' syntax but the _ confused me.Now, I previously thought that there would have to be a file _.py in the GTG package folder, but I guess that's wrong.the '__init__.py' file in the GTG/ folder has the line' _ = translation.gettext'so I guess that is all that _ is then.Any reason somebody would want to name a function in such a weird way?
Paul
@Paul W - yeah, I should probably do the same!
Paul
+3  A: 

from GTG import _ imports the _ function from the GTG module into the "current" namespace.

Usually, the _ function is an alias for gettext.gettext(), a function that shows the localized version of a given message. The documentation gives a picture of what is usually going on somewhere else in a module far, far away:

import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print _('This is a translatable string.')
badp
I hope SO won't get sued because of people like me quoting stuff off the copyrighted Python documentation...
badp
Hi thanks, this is really helpfull. I was aware of the general 'from BLA import bla' syntax so finding out about this convention was really what I was looking for! Using such a shorthand makes much more sense now. Thank you
Paul
@bp Fair use. Besides, reading the documentation is waaaaay more highly valued in the python culture than asserting property rights is...
twneale
I figured, @Paul, I was stumped as well the first time I saw it :)
badp