In C++ you can often drastically improve the readability of your code by careful usage of the ‘using’ keyword, for example:
void foo() { std::vector< std::map <int, std::string> > crazyVector; std::cout << crazyVector[0].begin()->first; }
becomes
void foo() { using namespace std; // limited in scope to foo vector< map <int, string> > crazyVector; cout << crazyVector[0].begin()->first; }
Does something similar exist for python, or do I have to fully qualify everything?
I’ll add the disclaimer that I know that using has its pitfalls and it should be appropriately limited in scope.
As Bill said, Python does have the construction
but you can also explicitly specify which names you want imported from the module (namespace):
This tends to make the code even more readable/easier to understand, since someone seeing an identifier in the source doesn’t need to hunt through all imported modules to see where it comes from. Here’s a related question: Namespace Specification In Absence of Ambuguity
EDIT: in response to Pax’s comment, I’ll mention that you can also write things like
but then you’ll need to write
instead of just
This is not necessarily a bad thing, of course. I usually use a mixture of the
from X import yandimport X.yforms, whatever I feel makes my code clearest. It’s certainly a subjective thing to some extent.